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

[Text] Get the system font instead of Helvetica programmatically and add a virtual fontName called "System" #1635

Closed
wants to merge 3 commits into from

Conversation

dalinaum
Copy link
Contributor

Get the system font instead of Helvetica programmatically and add a virtual fontName called "System" that defaults to whatever the current system font is.
#1611

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Jun 15, 2015
@dalinaum
Copy link
Contributor Author

I have to modify [RCTConvert_UIFontTests testSize] and [RCTConvert_UIFontTests testStyle].

@ide
Copy link
Contributor

ide commented Jun 15, 2015

This looks good for now but we'll later want to go further and use the system UIFont object instead of reconstructing it by hand because there are special flags set on it (getting the system font != creating a UIFont object with the same font name and size).

@dalinaum
Copy link
Contributor Author

@ide How can it go futher? Do you have any idea?

@dalinaum
Copy link
Contributor Author

Well. I got diffrent images.
2015-06-16 7 21 05

@ide
Copy link
Contributor

ide commented Jun 15, 2015

re: going further -- in +[RCTConvert UIFont:...], instead of building up the font object from [UIFont init...], I want to try starting with [UIFont systemFont....] and modifying the font/font descriptor's family, size, etc. so that all of the other system properties of the font (e.g. natural-spaced instead of monospaced numbers) are preserved. Alternatively (and eventually) all of those properties should be exposed to JS... this is one of those areas where iOS leaves Android way behind so no sense trying to provide perfect cross-platform compatibility.

@dalinaum
Copy link
Contributor Author

I reduce my aim. I don't know how can I get Helvetica Neue programmatically. systemFontOfSize returns Helvetica.

I agree your idea. But it looks to me like It's beyond my limit now.

@dalinaum dalinaum changed the title [Text] Get the default font family programmatically. [Text] Get the system font instead of Helvetica programmatically. Jun 15, 2015
@dalinaum
Copy link
Contributor Author

When I used [UIFont boldSystemFontOfSize:14], I got Helvetica NeueUI.
When I used [UIFont boldSystemFontOfSize:0], I got Helvetica.

@ide
Copy link
Contributor

ide commented Jun 15, 2015

I agree your idea. But it looks to me like It's beyond my limit now.

yeah, I think it's fine if this commit focuses just on getting iOS 9 on SF instead of Helvetica Neue.

@paramaggarwal
Copy link
Contributor

@dalinaum This is nice and solves one part of the problem which is using systemFont as the default font.

In addition to this, in RCTConvert which allows you to create a new/different font - @ide's suggestion is to start with a base systemFont object and then modify the fontDescriptor. Currently, it creates a font by name if no family is provided. See this highlighted code.

P.S. All the places where Helvetica is mentioned in code.

@nicklockwood nicklockwood self-assigned this Jun 16, 2015
@nicklockwood
Copy link
Contributor

I think we need to solve this universally, not just in TextView, as per discussion in #1611

@dalinaum dalinaum changed the title [Text] Get the system font instead of Helvetica programmatically. [Text] Get the system font instead of Helvetica programmatically and add a virtual fontName called "System" Jun 17, 2015
@dalinaum
Copy link
Contributor Author

Because 98fa688 generates different snapshot images, it breaks all snapshot tests. How can I pass thoses tests? (I added @ide @nicklockwood @paramaggarwal on my fork. If you want to modify my fork, you can modify my fork.)

Summary:
Get the system font instead of Helvetica programmatically.
@dalinaum
Copy link
Contributor Author

I modified snapshot images.

@nicklockwood
Copy link
Contributor

This is only a partial solution, as generating the system font from the family name won't include the other attributes @ide mentioned.

For the tests, we'll need to have different snapshots per OS version (which was already the case, as Apple always tend to make minor changes to font and UI rendering between OS versions). For now we can probably just mandate that the tests should be run on 8.x.

For apps, the expectation is that anything using system font will look different on new iOS releases, so I think that's OK. If people really want to keep using Helvetica (and make their app look dated on iOS9), they can specify the font explicitly.

@paramaggarwal
Copy link
Contributor

@nicklockwood 👍

Add a virtual fontName called "System" that defaults to whatever the
current system font is. (@nicklockwood facebook#1611)

It may break UI of existing apps.
@dalinaum
Copy link
Contributor Author

It generates the System font including some attributes(fontWeight, italic and condensed) now.

@paramaggarwal
Copy link
Contributor

@dalinaum starting to take shape. :)

@dalinaum
Copy link
Contributor Author

https://travis-ci.org/facebook/react-native/jobs/67305503

/Users/travis/build/facebook/react-native/Examples/UIExplorer/UIExplorerIntegrationTests/IntegrationTestsTests.m:82: failed: caught "NSInternalInconsistencyException", "*** Assertion failure in -[RCTTestRunner runTest:module:initialProps:expectErrorBlock:], /Users/travis/build/facebook/react-native/Libraries/RCTTest/RCTTestRunner.m:108: RedBox error: Could not connect to development server. Ensure node server is running and available on the same network - run 'npm start' from react-native root

Could not connect to development server.
It looks to me like that this error is not relevant to my commit.

@dalinaum
Copy link
Contributor Author

Travis works well now.

@nicklockwood I pushed another commit that uses fontDescriptor. Unfortunately, it looks like dynamic weight change is not supported in iOS.

fontWeight = [self RCTFontWeight:weight] ?: fontWeight;

if (weight) {
font = [UIFont systemFontOfSize:fontSize weight:fontWeight];
Copy link
Contributor

Choose a reason for hiding this comment

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

This method was only added in iOS 8.2, it will crash on iOS 8.1 and earlier (we need to support 7+)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh. I see. I have to modify that line.

@dalinaum
Copy link
Contributor Author

Pushed a modified commit again :)

@nicklockwood
Copy link
Contributor

The lack of dynamic weight change support was why I used the (rather hacky) solution of enumerating fonts to find the closest match with the requires attributes.

But we can't really ship an update that breaks fontWeight support for the system font. There must be a better solution.

@dalinaum
Copy link
Contributor Author

How about introducing [[UIDevice currentDevice] systemVersion]? This will protect fontWeight suppor fot the system font on iOS 8.2 and later. And we will use the soulution of enumerating fonts to find the closest match too on iOS 8.1 and earlier.

Some codes use __IPHONE_OS_VERSION_MIN_REQUIRED including RCTConvert. I think it would be another solution.

PS: The last commit(3ca2f73) is not relevant to this comment. I have had a mistake. I fixed it. That's it.

 * All font + weight/fontName -> It uses familyName to generate font.
 * System font -> It uses fontDescriptor to generate font.
 * Other font -> uses fontDescriptor to getnere font.

It looks like iOS doesn't support changing weight dynamically.
System font with weight is supported iOS 8.2 and later.
@nicklockwood
Copy link
Contributor

You'll probably notice I made a few changes to this before landing it, but I managed to keep support for bold/italic on system fonts and retain backwards compatibility with 8.2

@dalinaum
Copy link
Contributor Author

@nicklockwood I learnt something from your reviews and changes. I should learn more about React Native. Thanks so much.

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.

5 participants