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;
}
Related
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
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;
Can anybody please explain how can we make any font family font, bold or unbold + Italic or Non Italic + Underlined or Non underLined. Everywhere I got the method that make the changes but on system font. I even tried giving 2 attributes to NSAttributed string
1. Bold
2. A font family from list of supported font family
But it didnt work. Thanks in Advance.
try this
UIFontDescriptor *fontDescriptor = [[UIFontDescriptor alloc] init];
UIFontDescriptor *fontDescriptorForHelveticaNeue = [fontDescriptor fontDescriptorWithFamily:#"Helvetica Neue"];
UIFontDescriptor *symbolicFontDescriptor = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFontDescriptor *symbolicFontDescriptor1 = [fontDescriptorForHelveticaNeue fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitMonoSpace];
NSString *text = #"iOS 7";
if(some condition){
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor size:17.0f]}];
}
else{
CGSize fontSize = [text sizeWithAttributes:#{NSFontAttributeName:[UIFont fontWithDescriptor:symbolicFontDescriptor1 size:17.0f]}];
}
You can't do it with a few lines of code. A normal font and its bold version are completely separated (ie they're like two unrelated fonts), and if you have a look at iosfonts, the naming is not consistent. Some fonts don't have bold version, and some have several bold versions!
A solution (that requires a bit of effort, but surely works): create a list of pairs of font names, like this
{"ArialHebrew", "ArialHebrew-Bold"},
{"AvenirNext-Regular", "AvenirNext-Bold"},
...
And populate the list of fonts with the regular version (the left one). If the user desires to make it bold, then switch to the bold version (the right one).
My recommendation is to limit the number of choices for user (as iOS always does): you don't need to copy the whole list from iosfonts! just some popular ones are enough.
This may not be sufficiently generic for your purses, but maybe it'll help someone else
+ (UIFont *)whateverInvertedBoldnessFontFromFont:(UIFont *)font pointSize:(CGFloat)pointSize
{
NSString *customFontFamilyName = #"Whatever";
NSString *ibFontName = font.fontName;
NSString *customFontStyle = nil;
if ([ibFontName rangeOfString:#"Bold"].location != NSNotFound) {
customFontStyle = #"Regular";
}
else {
customFontStyle = #"Bold";
}
UIFont *customFont = [UIFont fontWithName:[NSString stringWithFormat:#"%#-%#", customFontFamilyName, customFontStyle] size:pointSize];
return customFont;
}
unfortunately this is quite fragile and works reliably when the source and destination
font families are known
If you want to set it programmatically, you must check with the font supported by xCode (iOS).
and if you want to do bold to any font then you have to use :
UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[myLabel setFont:boldFont];
where myLabel is your label name.
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.
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.