How to set font name of UILabel as HelveticaNeue Thin in iOS? - ios

I am creating UILabel, for the label i can set the font name as HelveticaNeue Regular, Light, UltraLight etc, But i unable to set the font name as HelveticaNeue Thin, it is not working as expected. I did like,
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16];
Also i have searched on Google didnt got any solution. How to fix this issue? Thanks.

This font is bundled with iOS 7, so if you're targeting iOS 7 and above your
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:16.0f];
will work.
However if you are targeting iOS 6.1 and below you'll need to embed the font

Updated answer to support swift
let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!

UIFontDescriptor *helveticaNeueFamily =
[UIFontDescriptor fontDescriptorWithFontAttributes:
#{ UIFontDescriptorFamilyAttribute: #"Helvetica Neue" }];
NSArray *matches =
[helveticaNeueFamily matchingFontDescriptorsWithMandatoryKeys: nil];
The matchingFontDescriptorsWithMandatoryKeys: method as shown returns an array of font descriptors for all the Helvetica Neue fonts on the system, such as HelveticaNeue, HelveticaNeue-Medium, HelveticaNeue-Light, HelveticaNeue-Thin, and so on.
You can modify the fonts returned by preferredFontForTextStyle: by applying symbolic traits, such as bold, italic, expanded, and condensed. You can use font descriptors to modify particular traits, as shown in Listing 9-2.
referenced by apple
or
this font you can't apply directly.
so you can customize your font
How to use custom fonts in iPhone SDK?

This has worked for me. Write this code below your label declarations.
It sets all the UILabel under a function with same font.
[[UILabel appearance]setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:32.0f]];
To set font to particular UILabel use this code :
[labelName setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:15.0f]];

Related

iOS : Mistake with setFont

I'm trying to use setFont on a label, but it seems it doesn't work on iOS > = 8.
I wrote :
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Light" size:12];
[self->myLabel setFont:font];
Any ideas ?
EDIT : The problem isn't on the font, but on the SIZE. The label stay on the "default" size wrote in the storyboard.
It's because there is no font family with name #"HelveticaNeue-Light" hence size:12 will also not work
And also make sure adjustsFontSizeToFitWidth is disabled with
label.adjustsFontSizeToFitWidth = NO
You can get the available fonts in attribute inspector of Interface Builder

Possible to detect Bold Text setting in Settings > Accessibility?

With iOS 7, it's possible to code your app to respect the user's setting for Dynamic Type - larger or smaller font sizes. You use the method preferredFontForTextStyle: and then listen to notifications in order to update the UI if the user changes the setting while your app is running. I am wondering if it's possible to do the same thing with the accessibility option "Bold Text" found in Settings > Accessibility. I realized that the Bold Text option actually requires you to restart the device, so there should be no need to listen to notifications because your app will be killed and relaunched anyways.
This is what I ultimately want to accomplish: I would like to change the navigation bar title text to a lighter style font. It may not be the default System font - it could be any font iOS can display, but I'll probably use HelveticaNeue-Light. I would also like to respect the user's preference for Bold Text. If it's enabled, I want to change the title text to a heavier weight of that same font - just like iOS does by default even though the default is already quite heavy - Helvetica Neue Medium. Indeed it does make it a little bit heavier when enabled. I want to do the same with a different font.
Here's what I'm doing to change it, but this obviously will be fixed no matter what the bold setting is:
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:#"HelveticaNeue-Light" size:17], [NSFontAttributeName, nil]];
I may have a solution but it seems to be a bad approach. I'm making a new font with a fixed size from the preferredFont for subheadline. This does almost exactly what I want - it automatically takes care of font-weight based on the Bold Text setting (HelveticaNeueRegular [I actually want Light] when disabled, HelveticaNeueMedium when enabled), but won't work for a different typeface. Perhaps there is a better approach?
UIFont *subtitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
UIFont *titleFont = [subtitleFont fontWithSize:17];
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
As of iOS 8, it is possible to detect whether the user has enabled Bold Text in Settings using UIAccessibility.isBoldTextEnabled (docs) and UIAccessibility.boldTextStatusDidChangeNotification (docs).
For apps that also require iOS 7 support, I’ve written an elegant one-liner that works on iOS 7 & 8 with Helvetica Neue and even on iOS 9 with the San Francisco typeface, based on the fact that standard-weight fonts are commonly referred to as the “Regular” weight, and that body text uses this weight for readability:
Objective-C:
BOOL hasBoldText = ![[UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontName hasSuffix:#"-Regular"];
Swift:
let hasBoldText = !UIFont.preferredFontForTextStyle(UIFontTextStyleBody).fontName.hasSuffix("-Regular")
You can use UIFontDescriptor for that:
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:17]; // better to use a constant
If you want to change when the font size changes, you can observe the UIApplicationContentSizeDidChangeNotification. I'm not sure if the Bold Text setting also sends this notification, but you can always update on applicationWillEnterForeground:. 99% of the time you update unnecessarily that way, but it should work if the user does decide to change it.
I found another solution. Just parse the current title font to see if it contains the substring 'bold' and if it does not find it, then you know Bold Text is disabled, and you can apply your custom font. Note that this would stop working if Apple changed the heading weight. For example, took it down one notch to Regular and Medium instead of Medium and Bold. And if Apple changes the font family, your fixed font won't match it obviously. But it doesn't seem to be a terrible solution.
UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:#"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
UIFont *titleFont = [UIFont fontWithName:#"HelveticaNeue-Light" size:17];
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
//put custom font here for when Bold Text is enabled, or do nothing to get the default
}
In iOS 7 and up, I noticed that the UIFontTextStyleHeadline is: HelveticaNeueInterface-Heavy.
I modified the op's response as follows:
UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:#"bold" options:NSCaseInsensitiveSearch].location == NSNotFound && [currentTitleFont.fontName rangeOfString:#"heavy" options:NSCaseInsensitiveSearch].location == NSNotFound) {
UIFont *titleFont = [UIFont fontWithName:#"HelveticaNeue-Light" size:17];
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
//put custom font here for when Bold Text is enabled, or do nothing to get the default
}
Try this one. It works for me.
let hasBoldText = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body).fontName.contains("bold")
UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body).fontName
11.4 12.0
".SFUIText"
".SFUIText-Semibold"
13.3

".SFUI-Regular"

".SFUI-Semibold"

setting a font seperatly for ios5 and lower

I have recently started using avenir as the font for my button text in my storyboard. It looks normal when I run it on ios6 , however in ios5 and lower as it doesn't support avenir it just uses the system default which looks awful aswell as the text not all fiting in the button, like so
Can anyone tell me how I would select a different font and text size for ios5, or a different font and text size when it returns nil to avenir?
UIFont *font = [UIFont fontWithName:#"iOS 6 font" size:SIZE];
if (!font) {
font = [UIFont fontWithName:#"Legacy font" size:SIZE];
}

UILabel - set custom fonts

I want to add GillSans-Bold font to a UILabel.
I have set it in the xib file , and I'm also setting it up in my class as follows :
[label setFont:[UIFont fontWithName:#"GillSans-Bold" size:18]];
But , it doesn't seem to work for me.
Any suggestions ?
iPhone 4.3 doesn't have Gill Sans, but iPad has it since 3.2.1.
See this list comparing fonts for iPad 4.3 and iPhone 4.3. To be sure, this is how you get the list of fonts available on your device:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
If it says
GillSans
GillSans-Bold
GillSans-BoldItalic
GillSans-Italic
then [UIFont fontWithName:#"GillSans-Bold" size:18] should return a valid font.
For this to be working I had to add this font in my project directory , and added this font in the Info.Plist file
Does the font GillSans-Bold exist? Check if [UIFont fontWithName:#"GillSans-Bold" size:18] returns an UIFont, not null.
Swift
let label = UILabel()
label.font = UIFont(name: "Roboto-Regular", size: 16)
label.text = "Your text here"
P.S. Before that you must:
Add custom font in project
Add font names in Info.plist file
Add font files in Copy Bundle Resources
After that you can also use this fonts in storyboards

make UILabel's text bold

I want to make UILabel's text bold
infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(90,150, 200, 30)];
[infoLabel setText:#"Drag 14 more Flavors"];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont fontWithName:#"Arial" size:16]];
[infoLabel setTextColor:[UIColor colorWithRed:193.0/255
green:27.0/255
blue:23.0/255
alpha:1 ]];
If you want to retain the system font and make it bold:
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];
Try
[infoLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:16]];
It may also be worth checking if the font you're trying to use is available on device
Using the GUI in Xcode select the label then go to the Attributes Inspector. One of the options is Font. Click on the font icon (not the up-down arrows). In the popup that appears expand the Font ComboxBox. Under the Bold System section choose Regular.
For swift users this should work:
myLabel.font = UIFont.boldSystemFont(ofSize: 12.0)
or if you'd like to use a different font:
myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 12.0)
Where possible I would suggest using dynamic font sizes to provide the best possible accessibility to your users.
You can make a label use a system dynamic font and set it to have bold text by doing the following:
exampleLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .bold))
You can set the stroke with a negative value to make a bold effect if you don't have the bold variation of your custom font. See here:
How can I both stroke and fill with NSAttributedString w/ UILabel

Resources