How to use different font name in UITableViewCell UILabel - ios

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

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

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.

Set Multiple Properties Simultaneously

Is there any way to set multiple properties simultaneously? I tried this:
dateTextField.font, nameTextField.font = [UIFont fontWithName:#"Gotham-Book" size:16];
But that got a warning. Is there something else I could do?
How about just dateTextField.font = nameTextField.font = [UIFont fontWithName:#"Gotham-Book" size:16];??
You definitely cannot do that.
Consider using a loop or just assign the font twice.
UIFont * font = [UIFont fontWithName:#"Gotham-Book" size:16];
dateTextField.font = font;
nameTextField.font = font;
EDIT
Ok, I lied: multiple assignment exists in Objective-C, but please don't use it as it may lead to unexpected behavior and terrible code to maintain.

how to change the style of the font programmatically in objective-c

I would like to change the style of the font (like bold, regular, light, oblique) programmatically. I know I can use the IB, but I would like to change it using programmatically. Need some guidance on this. Sorry if it is a stupid question.
For example, my code looks like this:
lblAge.font = [UIFont fontWithName:#"Helvetica" size:20];
I would like to add the style which is regular in. How do I it?
This should get you going
offerTitle.font = [UIFont fontWithName:#"TimesNewRomanPS-ItalicMT" size:14.0f];//here offerTitle is the instance of `UILabel`
Hope this helps:)
Try this solution:
myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
myLabel.font = [UIFont italicSystemFontOfSize:16.0f];
For regular size:
myLabel.font = [UIFont systemFontOfSize:16.0f];
Hope it helps you.
For iOS 8.2 and above there is + systemFontOfSize:weight: which allows you to specify weighted system fonts.
Look at API of UIFont. Assign created font to the property 'font' of designated object
oneLabel.font=[UIFont fontWithXXX];
static NSString *_myCustomFontName;
+ (NSString *)myCustomFontName:(NSString*)fontName{
if ( !_myCustomFontName ){
NSArray *arr = [UIFont fontNamesForFamilyName:fontName];
// I know I only have one font in this family
if ( [arr count] > 0 )
_myCustomFontName = arr[0];
}
return _myCustomFontName;
}

Resources