Set Multiple Properties Simultaneously - ios

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.

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;

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.

Changing UILabel font

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.

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