Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add letterSpacing style property for Text #482

Closed
wants to merge 1 commit into from

Conversation

vkurchatkin
Copy link
Contributor

Fixes #457

@frantic
Copy link
Contributor

frantic commented Mar 30, 2015

Restarted the Travis build. @a2 / @nicklockwood - what do you think?

@brentvatne
Copy link
Collaborator

👍 - I'd love to see this get merged

@@ -105,6 +111,8 @@ - (NSAttributedString *)_attributedStringWithFontFamily:(NSString *)fontFamily
[self _addAttribute:RCTReactTagAttributeName withValue:self.reactTag toAttributedString:attributedString];
[self _setParagraphStyleOnAttributedString:attributedString];

[self _addAttribute:NSKernAttributeName withValue:@(letterSpacing) toAttributedString:attributedString];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tiny nit, but I would move this to after the line that adds the NSFontAttributeName attribute to maintain consistency but that's really the only thing I have against this. Looks good otherwise! 👍

@vkurchatkin vkurchatkin force-pushed the text-letter-spacing branch 2 times, most recently from 06e01e6 to d1451fe Compare March 30, 2015 21:54
@vkurchatkin
Copy link
Contributor Author

@a2 thank you, fixed the nit

@vjeux
Copy link
Contributor

vjeux commented Mar 30, 2015

Hey, can you make sure it actually follows the same behavior as CSS.

Example, can you run this in React Native, take a screenshot and make sure it looks pixel perfect:
http://jsfiddle.net/y7h9j3uw/

If you want to try a different font: http://iosfonts.com/

@vjeux
Copy link
Contributor

vjeux commented Mar 30, 2015

It is super important that any feature which has the same name as CSS does actually work exactly the same as CSS, otherwise it makes debugging a lot hard and confuses developers.

@vkurchatkin
Copy link
Contributor Author

@vjeux here is what I get:

screenshot

The code:

return (
      <View style={{ flex: 1, backgroundColor: 'white' }}>
        <View style={{ flex: 1, justifyContent: 'flex-end', alignItems: 'flex-start' }}>
            <Text style={{ fontFamily: 'Copperplate', letterSpacing: 5, fontSize: 10 }}>
              This is <Text style={{ letterSpacing: 0 }}>a text with a lot</Text> of spaces
            </Text>
        </View>

        <View style={{ flex: 1, backgroundColor: 'blue' }}>
          <WebView url="http://localhost:8082/" renderLoading={() => <View/>} renderError={() => <View/>}/>
        </View>

      </View>
    );
  }

Html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body style="padding: 0; margin: 0">
  <span style="font-family: Copperplate; letter-spacing: 5px; font-size: 10px;">
      This is <span style="letter-spacing:0">a text with a lot</span> of spaces
  </span>
</body>
</html>

@vjeux
Copy link
Contributor

vjeux commented Mar 30, 2015

Can you also check what's the behavior for negative spacing? Thanks a lot for making sure it's the same

@vkurchatkin
Copy link
Contributor Author

For -3px I get the following:

screenshot

Pretty close

@vkurchatkin
Copy link
Contributor Author

There is the last nasty problem: width calculation. One the web letter spacing affects width, both positive and negative. Negative letter spacing makes width less than required to display text, but it is not clipped according to this width (even with all kinds of overflow and text-overflow properties). On iOS, on the contrary, it is clipped.

@vkurchatkin
Copy link
Contributor Author

Pushed a fix for this problem, now behaves exactly the same way

@@ -32,6 +32,7 @@ var TextStylePropTypes = Object.assign(Object.create(ViewStylePropTypes), {
writingDirection: ReactPropTypes.oneOf(
['auto' /*default*/, 'ltr', 'rtl']
),
letterSpacing: ReactPropTypes.number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you add a trailing comma

@brentvatne
Copy link
Collaborator

Great work @vkurchatkin - looking forward to putting this to use!

@vkurchatkin
Copy link
Contributor Author

@vjeux all done

@chirag04
Copy link
Contributor

chirag04 commented Apr 2, 2015

Awesome work @vkurchatkin - I badly want to see this getting merged.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 7, 2015
@vkurchatkin
Copy link
Contributor Author

@vjeux ping

@nicklockwood
Copy link
Contributor

This looks good to me. Not sure what's holding it up. I'll check.

@ide
Copy link
Contributor

ide commented May 8, 2015

+1

@brentvatne
Copy link
Collaborator

@nicklockwood any update?

@nicklockwood
Copy link
Contributor

Sorry about the radio silence. There were a few issues merging - notably this broke the UIExplorer snapshot tests because the default letterSpacing value of 0 is not pixel-for-pixel identical to having no letterSpacing set at all (it disables iOS's per-character smart kerning).

Anyway, that's fixed now, and I've merged this internally so it should be appearing on GitHub real soon.

@brentvatne
Copy link
Collaborator

🌴 that's great, thanks!

vjeux pushed a commit to vjeux/react-native that referenced this pull request May 13, 2015
Summary:
Fixes facebook#457
Closes facebook#482
Github Author: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>

Test Plan: Imported from GitHub, without a `Test Plan:` line.
@vjeux vjeux closed this in a142ed5 May 13, 2015
jfrolich pushed a commit to jfrolich/react-native that referenced this pull request Apr 22, 2020
ayushjainrksh referenced this pull request in MLH-Fellowship/react-native Jul 2, 2020
* View Properties accessibilityRole/States

Added documentation for accessibilityRole and accessibilityStates on View

* Update view.md

* Update view.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Letter spacing CSS property
9 participants