Reputation: 191
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.
Upvotes: 1
Views: 5840
Reputation: 12023
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]}];
}
Upvotes: 7
Reputation: 91
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.
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 11132
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.
Upvotes: 0