Using styles of the iOS system font other than Regular in code - ios

At the moment I know I can use [UIFont systemFontOfSize:15] to set the font of a UILabel to the system font. But how would I retrieve the medium style of the system font programmatically? There only seems to see a method with the regular style of the font to be applied.
At the moment I can only seem to use these fonts in IB:

You can use +systemFontOfSize:weight:, with one of the font weight constants from UIFontDescriptor.
Or, perhaps more appropriately, preferredFontForTextStyle: which respects the user's settings.

Related

How to get iOS font size and set it to all labels of the app?

I want to make an app, where font size increase or decrease according to font size maintained in iOS in Settings. If you change font size of your iOS from settings, whatsapp font size displayed accordingly, I want same functionality.
The iOS functionality you're looking for is the Dynamic Type that only works for text with implemented text styles.
Basically, you must :
Use the text styles but beware, their availability depends on your iOS version.
Tick the adjustsFontForContentSizeCategory property (since iOS 10) in your interface builder or implement it in code so as to tell the system to handle automatically the Dynamic Type for the object it belongs to (text styles must be used of course).
Adapt all your constraints to the different sizes your app may encounter.
You can also follow the notifications related to the Dynamic Typeevents as indicated below :
Everything is well explained in this WWDC video detailed summary where all the contents and their video timelapses are indicated to reach rapidly the information.
There's also the possibility of adapting the graphical elements size as well with a Dynamic Type implementation.
All you have to do if to use Dynamic Type for your labels. This means not to set it explicitly but to use styles like Header 1 or caption. This styles are depends on user setting in Accessibility and will change automatically. https://www.raywenderlich.com/77092/text-kit-tutorial-swift
You can use system default sizes for Texts, like,
self.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
Please find documentation here.
For other components you can use like labelFontSize() and many more,
+ (CGFloat)labelFontSize;//Returns the standard font size used for labels.
+ (CGFloat)buttonFontSize;//Returns the standard font size used for buttons.
+ (CGFloat)smallSystemFontSize;//Returns the size of the standard small system font.
+ (CGFloat)systemFontSize;//Returns the size of the standard system font.

How to get the UIWebView's equivalent of the iOS 8 system font?

I am trying to use the very same font in a UIWebView on iOS that the iOS system uses currently.
This means I want to use the very same font I'd get from:
UIFontDescriptor *fd;
fd = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFont *font = [UIFont fontWithDescriptor:fd size:0];
There are lots of answers on SO (iPhone Development - Setting UIWebView font, Using custom font in a UIWebView, How to change UIWebView default font, How to set custom font in UIWebView?) that explain how to embed a UIFont's familyName or fontName into an html's style attributes, by doing this:
NSString *fontStyle, *htmlString = #"test";
fontStyle = [NSString stringWithFormat:#"font-family:%#;font-size:%d", font.familyName, (int)font.pointSize];
htmlString = NSString stringWithFormat:#"<span style=\"%#\">%#</span>", fontStyle, htmlString];
But this doesn't seem to work on iOS 7 and 8 any more where these system font names are now like:
font.fontName: .HelveticaNeueInterface-Regular
font.familyName: .Helvetica Neue Interface
When using these names in an UIWebView with the "font-family" attribute, the text gets rendered in the default font (which is with serifs and certainly not the new iOS system font). Removing the leading period doesn't help, either.
So, how do I now translate the system's font into a name that gets correctly interpreted by the Web Kit?
Please understand that I am seeking for a generic solution and do not simply seek the iOS 7/8 name I can insert manually into the html style attribute.
I just ran into this blog article which may offer the solution though I have not verified it yet:
If you're trying to reference the system font in web views just prepend -apple-system to your font-family list and it'll use SF in iOS 9 and Helvetica in older versions.
Which means that the font line of above code needs to be change to:
fontStyle = [NSString stringWithFormat:#"font-family:-apple-system,Helvetica Neue;font-size:%d", (int)font.pointSize];
That should take care of selecting the system font from iOS 7 onward.
what about this article: http://furbo.org/2015/07/09/i-left-my-system-fonts-in-san-francisco/ from Craig Hockenberry?
Have you tried Helvetica or San Francisco with attaching font files?

Which font / font size is used by apple for the placeholder in iOS

I try to clone a placeholder behavior for a tableCell. It must look exactly like the placeholder used within a UITextField. Does anybody know which Font is used by Apple as well as which Font size?
Thanks
The default font in UITextfield is HelveticaNeue 14

How do I make DTCoreText use the System Font?

I want to use DTCoreText in my app to convert HTML to NSAttributedString, but I can't figure out how to use the system font (as of iOS 8: Helvetica Neue) as the base font. By default DTCoreText seems to create the NSAttributedString with the Times New Roman font.
I'd really prefer not to hardcode it. If Apple updates their font again (they previously updated it from Helvetica to Helvetica Neue) I don't want to replace all instances of it, or have a bunch of conditionals depending on the OS version running the code.
I tried:
NSDictionary *options = #{
DTUseiOS6Attributes: #(YES),
DTDefaultFontFamily: [UIFont systemFontOfSize:[UIFont systemFontSize]].fontName
};
But it still renders as Times New Roman.
How do I make it use the system font? I just want it to look normal.
Looks like DCCoreText is expecting to receive font family name, and you're providing font name which is different. Try to replace your code with
[UIFont systemFontOfSize:[UIFont systemFontSize]].familyName

iOS - Is hard coding the font / font size bad?

I'm looking to determine how well internationalized an iOS application is, and came across a few instances of code like this:
[_lblTitle setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0]];
On other platforms, I would say this is not best practice. For example, if I wanted to decrease the font size for certain languages (due to text expansion), or if I wanted to remove the bolding (e.g. for double byte languages), it would require a code change.
Question - Is this best practice for iOS, and if not what is the alternative?
Disclaimer - I'm not at all familiar with iOS development.
iOS is not as versatile as other platforms for those kind of things, and setting the size and the font in code is a common practice, either in your class or in a external Contanst class, or just set them in Interface Builder if you are using it.
As a personal preference, setting the fonts in Interface Builder makes them harder to see, I prefer to have all the fonts set in my class and see them together. Then, if I change a font, the rest of them will change at the same time, I dont have to go one by one with all the elements in the xib.
In any of the solutions you'll have to recompile every time you change the font.
What I can think in terms of adapting it for different languages could be resizing the label where the text is set doing something like:
myLabel.minimumFontSize = 8.;
myLabel.adjustsFontSizeToFitWidth = YES;
This will shrink the text down to 8 as minimun in case it doesn't fit.
Another practice is to detect the language of your phone and setting the font there, for example:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
And depending on the result do this for language:
[_lblTitle setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0]];
and this for example for another:
[_lblTitle setFont:[UIFont fontWithName:#"HelveticaNeue" size:15.0]];

Resources