Changing UILabel font - ios

Say I have three UILabels.
I create a font and assign that font to
all three labels.
Now I edit this font's type/size etc and expect the fonts of those labels to change too, which does not happen. I basically need to apply a single font to multiple labels and I know this can be done with an outlet collection but i'd just like some help in understanding the flaw in my logic here so here's my code...
self.labelFont = [UIFont fontWithName:#"System" size:12];
self.label1.font = self.labelFont;
self.label2.font = self.labelFont;
self.label3.font = self.labelFont;
self.labelFont = [UIFont fontWithName:#"Courier" size:30];
Thank

In your code you create two objects, [UIFont fontWithName:#"System" size:12], and [UIFont fontWithName:#"Courier" size:30].
Initially, your labelFont variable points to the first font object. Then you copy that pointer in the assignments to fonts of the three labels. At this moment, you have four pointers referencing the same object - namely, the object returned by the [UIFont fontWithName:#"System" size:12] call.
Next, you change labelFont to point to [UIFont fontWithName:#"Courier" size:30]. Once you do that, the labelFont starts pointing to that new object; it no longer points to the old object. However, the three labels are still pointing to the old font, so their appearance does not change.

This can not be done immediately, you would require a pointer to pointer able to change the font of every label. But when a setter is called it triggers a method that may also cause some side effects, so the better way to do this is to create a setter to handle the behavior:
- (void) setLabelFont: (UIFont*) labelFont {
_labelFont= labelFont;
self.label1.font = self.label2.font = self.label3.font = labelFont;
}

You should just subclass
Y
UILabel, modify the font there and then any labels that you make part of the subclass will have your font. It is less coding and can save you headaches in the long term when you have 100+ labels you want to make the same font.

Related

IOS Objective C UILabel font size is keeping its storyboard setting

I am using objectiveC for an old project and I am setting the label font size programmatically like this :
UIFont *font = [self fontToFitHeight:NSLocalizedString(#"#61_NEW_FINAL", nil) font:self.rulesLabel.font size:self.rulesLabel.frame.size.height];
[self.rulesLabel setFont:font];
NSLog(#"after call : fontszie is : %lf",self.rulesLabel.font.pointSize);
I can include the helper function if needed, but it returns me what I want, which is a font with fontsize around 5, which i see printed.
But when running that my font size is definitely not 5, but instead whatever value I have in the Storyboard settings (in my case 16). Above code is called inside viewDidLoad.
What am I missing?
NSAttributedString can include one or more fonts, perhaps this is your issue. If you have set the text of the label to be an attributed string that contains a font or a color, changing the base font or color of the label will not make a difference. In order for it to work as expected, you can either remove any font formatting of your attributed string (that I assume that you set through IB) or recreate the string in code and add the proper font to it, like so:
NSString *text = NSLocalizedString(#"#61_NEW_FINAL", #"Don't leave this out");
UIFont *font = [self fontToFitHeight:text font:self.rulesLabel.font size:self.rulesLabel.frame.size.height];
NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:text];
[labelAttributes addAttribute:NSFontAttributeName
value:font
range:NSMakeRange(0, labelAttributes.length)];
At this point, changing the font of the label will make absolutely no difference to its apperance, since the entire text has a predefined font already.

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

Detecting light fonts in iOS

I am trying to specify the font family of every label in my iOS app in a way that makes it fairly easy to change them later. I don't want to have to go through Interface Builder and reset every font on every screen. According to this post, I have created a method that will find all the fonts in a view and set them accordingly.
In my case, there are a few different font families I need to use based on whether the font is bold, italic, or light (e.g. skinny). These are all located in separate files such as "OpenSans-Semibold.ttf", "OpenSans-Italic.ttf", and "OpenSans-Light.ttf".
Ideally, I would like to be able to set the font to bold, italic, or light in Interface Builder, then have the code override just the font family, using the appropriate .ttf file. According to this post, I can pretty easily detect whether the font has been set to bold or italic, but finding out whether it's light or not doesn't seem to be working.
For the light fonts, the value of "traits" is 0x0--so no flags are set. Is there another way to detect light fonts?
Code looks like this:
- (void) setFontFamily:(UIView*)view
{
if([view isKindOfClass:[UILabel class]])
{
UILabel* label = (UILabel*)view;
UIFontDescriptorSymbolicTraits traits = label.font.fontDescriptor.symbolicTraits;
BOOL bold = traits & UIFontDescriptorTraitBold;
BOOL italic = traits & UIFontDescriptorTraitItalic;
if(bold)
[label setFont:[UIFont fontWithName:#"OpenSans-Semibold"size:label.font.pointSize]];
else if(italic)
[label setFont:[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
else if(light)
[label setFont:[UIFont fontWithName:#"OpenSans-Light"size:label.font.pointSize]];
else
[label setFont:[UIFont fontWithName:#"OpenSans"size:label.font.pointSize]];
}
for(UIView* subView in view.subviews)
[self setFontFamily:subView];
}
Your entire approach to determining a font based on its characteristics is problematic:
else if(italic)
[label setFont:
[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
You are hard-coding the font name based on the trait. Instead, ask the runtime for the font based on the name and trait. In this very simple example I find out what installed font, if any, is an italic variant of Gill Sans:
UIFont* f = [UIFont fontWithName:#"GillSans" size:15];
CTFontRef font2 =
CTFontCreateCopyWithSymbolicTraits (
(__bridge CTFontRef)f, 0, nil,
kCTFontItalicTrait, kCTFontItalicTrait);
UIFont* f2 = CFBridgingRelease(font2);
Note that this code is valid in iOS 7 only, where CTFontRef and UIFont are toll-free bridged to one another. In theory it should be possible to do this without C functions through UIFontDescriptor, but the last time I looked it was buggy and didn't work for all fonts (e.g. Gill Sans!).
That is what you should be doing: determine the symbolic and weight traits of your starting font, and then ask the runtime for the font that most close matches your requirements.

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.

Resources