iOS : Mistake with setFont - ios

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

Related

How to use different font name in UITableViewCell UILabel

I want to use different Font name in a UILable in UITableViewCell. I try with a lot of examples like:
Cell.cellSectionLabel.text = [self.textsInTableSection objectAtIndex:0];
Cell.cellSectionImageView.image = [UIImage imageNamed:[self.imagesInTableSection objectAtIndex:0]];
Cell.cellSectionLabel.textColor = [UIColor colorWithRed:59.0f/255.0f green:56.0f/255.0f blue:62.0f/255.0f alpha:1.0f];
[Cell.cellSectionLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
in cellForRowAtIndexPath
It is not working. The Font name actually read the default Font of my device. Because if I use some different Font name here [Cell.cellSectionLabel setFont:[UIFont fontWithName:#"Baskerville" size:16]];, it doesn't change any thing. It remain the same. How can I use different font name in this case?
Thanks a lot in advance.
You are setting it back to the system font. Delete this line:
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
Please remove the last line of your code and try again.
Cell.cellSectionLabel.font = [UIFont systemFontOfSize:16];
According to your code you are setting font to Helvetica 16
& again changing it back to System font

"Dynamic Type" Changing font size of the button

I am changing text size in the button when user selects prefered text size in the settings. I am doing it like that:
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.button.font = font;
It does work, but I get an error:
font is deprecated:first deprecated in IOS 3.0
I wonder if there is another, more proper way to achieve it.
You need to use self.button.titleLabel.font = font; instead (see docs).
Use
self.button.titleLabel.font = font;

How to set font name of UILabel as HelveticaNeue Thin in 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]];

Change the size of existing UILabel

I have a UILabel. Its name, size and other properties are set in IB.
I wish to change the font size if the text is longer than lets say 100.
All the examples I saw here I have to supply the text name but I'm looking for something of the sort:
label.font.size = 15;
I can't user [UIFont fontWithName:#"Arial" size:13.0f]] nor [UIFont systemFontOfSize:25]
I'm sure there must be a way to change only the size
label.font = [label.font fontWithSize:15];
label.font = [UIFont fontWithName:label.font.familyName size:15];
You should look at minimumFontSize and minimumScaleFactor depending on which versions of iOS you need to support.

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];
}

Resources