Different text bounding for NSAttributedString by platforms - ios

I used this code to measure size of an NSAttributedString.
static NSStringDrawingOptions const OPTS = NSStringDrawingUsesLineFragmentOrigin
| NSStringDrawingUsesFontLeading
;
CGRect b1 = [text boundingRectWithSize:targetSize options:(OPTS) context:nil];
This usually works well, but sometimes it shows incorrect result sometimes. Result size for same attributed-text object is about 10pt smaller than it actually rendered with UITextView, and I cannot choose correct size for the text-view.
Same code works well with simulators, but broke in actual device. Why this happens and what can I do to avoid this?
Test environment:
iPod 5th generation
iOS 7.1.2
Japanese language mode.

In my case, I built NSAttributedString with no attributes. Not even with NSFontAttributeName field. In this case, according to manual, system will choose predefined default font -- "Helvetica, 12pt".
But it seems the OS is choosing some different (which is wrong) sized font for the method in the environment. Maybe because of Japanese language mode. Anyway, UITextView rendering routine uses another font, and as a result, measured size becomes different with rendered size.
To avoid this, just set a font explicitly for EVERY characters in the attributed-string.
After specifying font for all characters explicitly, I confirmed that all the devices I have and simulators in Xcode 6 are working correctly. I believe there can be more odd cases if I don't set some metric-related attributes. Then I recommend to set all the metric attributes always.
I am not sure what will happen in other environment, but my case was solved with this. I hope this helps.
Update
I same issue occurs again even after applying this solution. This solution does not provide correct patch. Anyway this problem never appear on iOS 8.0, and I suspect this as a bug in the system framework.

Related

Understanding default font sizes in React Native

I've been working on a React Native app for the past few months, and something has always eluded me that I'm just now trying to get to the bottom of. I'm trying to standardize the font sizes within my app (for body, headers, etc.) and am struggling to understand where exactly React Native gets its default font size from. I've been testing in an iPhone 8 simulator running iOS 11, and through experimentation have come to the conclusion the default font displayed is 14 logical pixels (as in a brand-new element with no styling). However, checking the iOS style guidelines it would seem the default body text on iOS is intended to be 17pt. Although I don't a ton about device scaling, I've tried crunching the numbers in every online converter I could find, and could not come up with how 17pt may come close to 14 logical pixels (assuming RN uses the body font size by default). The only way I was able to roughly translate the two was if the DPI was around 90, but the DPI for iPhones is much, much higher. So might anyone be able to offer insight as to how default fonts are selected in React Native? Even hunting through the source code I couldn't find much. I'd like to be able to calculate the base size myself so I can scale the other font sizes according. Thanks in advance!
I suggest you to look at react-native-element's helper method normalizeText. It calculates the pixel ratio and set the texts fontSize accordingly.
Also you should also take in consideration of Accessibility Text Size Option in iOS. This option will be affecting all your app.

How to scale fontsize on different devices programmatically (Xamarin.android)?

I'm new to Xamarin and I'm actually developing an Android App. I've tried to run the app on different Android phones, but the fontSize appears either too small or large. Programmatically, how can I scale the FontSize of texts so that it can be adjust according to the device's screen?
Firstly, make sure you are using sizes in dp not px (or in, mm etc.) as these will factor in the screen density.
If you still have an issue, look at Screens support in the Android Docs, especially Using configuration qualifiers. Examples can be found here.
Basically you would need to provide a different resource file for each screen size. You should have a file something like res/values/dimens.xml where you store sizes for all your components. Make sure your font sizes are declared and referenced from here. You can then create a series of folders/files for the different screen sizes
res/values/dimens.xml
res/values-sw320dp/dimens.xml
res/values-sw720dp/dimens.xml
res/values-sw1024dp/dimens.xml
where each of these folders define the smallest width screen that that file will be used for.

Is it possible to change the font size of the app name displayed under the app icon?

I've seen apps that have more characters than mine and displayed fully whereas mine starts to truncate with the "..." after its 10th char.
I know an app on my iPhone that has 14 characters, and they are all displayed without any "...". I noticed that app's app title's fonts are a little bit smaller and the char-spacing in between is much less compared to the other apps.
Unfortunately you can't. The actual size isn't measured by chars because every char has a different width.
Your app name probably has more "boldy" chars that resulting in less char count than the one you mentioned.
BTW
The OS is shrinking app names when it tries to fit it completely under the app icon, but this also has a limit of course.
The font they use for application name is not monospace (or fixed-width). Means, the length of the name depends on the characters: M & W take more space than I & T.
Yes, there is no way to choose the font. But you might come up with a workaround.
As an option you can replace a whitespace with a thinner one as explained here.
Choosing the app name is very difficult. Find a short name. You can also put a part of your app name directly in the app's icon.

Attributed String UILabel text is shrunken

I am writing an app that requires labels w/ musical accidental symbols, called sharp and flat (♯ and ♭, respectively). Specifically, I need a label that says c♯. It is irrelevant to the question what these two symbols mean within a musical context. My app's main font is Open Sans. However, because Open Sans doesn't specify a character for the sharp and flat symbols, it uses a generic sharp and flat symbol that I find unattractive. However, I know that Helvetica Neue (or the iOS system font) has some well made symbols for sharp and flat, so I decided to change the label's text to an attributed string so I could make the c Open Sans, and the ♯ Helvetica Neue (both size 24). I did this through the interface builder. Even though everything looks exactly the way I want in the storyboard, when I launch it in the iOS Simulator, the labels with attributed strings are significantly shrunken from the other labels. I should note that the label has 3 AutoLayout constraints, though these contrasts are positionally related only, not size related.
Things I have tried (unsuccessfuly):
Using only one font
Doubling the font size (was too big)
Removing autolayout constraints
I would greatly appreciate it if someone could explain how to correctly create these labels. I am happy to provide any more information that is needed.
Thank you
I discovered that the problem is that NSAttributedString does not (yet) support custom fonts through Interface builder. It looks like I will have to subclass. This question essentially boils down to be the same as this one: Attributed string with custom fonts in storyboard does not load correctly

Photoshop Font Pixel Size to Xcode Interface builder Font Point Size

How do I match the font pixel size given to me by my designer in PhotoShop to the correct font size in Xcode Interface builder.
For example, my designer is using Helvetica Neueu Regular 32px Font in his design.
I've used a few points to pixel translation sites, but it doesn't seem exact.
I have attempted to follow the answer from this question, but to no avail:
https://stackoverflow.com/a/6107836/1014164
You will never have perfect results when visually comparing a Photoshop comp to a real program. In fact, it's not un-common for a text layout to be different between different computers because version and operating system differences (as well as monitor layouts) cause the text to reflow every time it's edited.
Unless things are very much different in other versions of Photoshop, your designer hasn't specified 32px because Photoshop doesn't lay text out in pixels - it works in points/picas. The exact text rendering is also dependent on the document's resolution (which is different between print and screen).
The best you can do is get the text to look roughly proportional to the designer's intent. In modern iOS, most apps will use the user's customized font settings anyway.

Resources