How to make UIFont bold or italic?

Having a UILabel with any font, how can I find out if it is already in bold? Or how can I make it bold? In CSS, I have a font-weight attribute. I would like to have something like that.

All that I have found out so far is that you must set the correct font name. However, this is unreliable. Cochin bold is Cochin-Bold , but the bold version of ArialMT not ArialMT-Bold , but Arial-BoldMT , so obviously adding -Bold not enough. (A bold version of a custom font may also have a completely different name.)

What I can do is find all the fonts for my given font family.

 __block UIFont *font = myLabel.font; [[UIFont fontNamesForFamilyName:font.familyName] enumerateObjectsUsingBlock:^(NSString *fontName, NSUInteger idx, BOOL *stop) { if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound) { font = [UIFont fontWithName:fontName size:font.pointSize]; *stop = YES; } }]; myLabel.font = font; 

But it does not work reliably. I can easily get the BoldItalic version. I could improve my check to avoid this, but this is not a good solution.

Maybe CoreText can help here?

+4
source share
3 answers

Maybe CoreText can help here?

CoreText uses its own CTFont font system. If you use this, you can do what you want:

 CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)name, size, NULL); CTFontRef boldFont = CTFontCreateCopyWithSymbolicTraits(font, 0.0, NULL, kCTFontBoldTrait, kCTFontBoldTrait); 

Suppose you could get the name of the resulting bold:

 CFStringRef boldName = CTFontCopyPostScriptName(boldFont); 

... and use it to create a new UIFont:

 UIFont *ret = [UIFont fontWithName:(NSString *)boldName size:size]; 

I don’t know how fast it would be, but you could do it when you start the application and then cache the names.

+10
source

UIFontDescriptorSymbolicTraits symbolically describes the stylistic aspects of the font. The upper 16 bits are used to describe the appearance of the font, while the lower 16 bits are for the font. Font appearance information, represented by the upper 16 bits, can be used for stylistic mapping of fonts.

Swift 3

 extension UIFont { convenience init?(name: String, size: CGFloat, symbolicTraits: UIFontDescriptorSymbolicTraits) { guard let descriptor = UIFontDescriptor(name: name, size: size).withSymbolicTraits(symbolicTraits) else { return nil } self.init(descriptor: descriptor, size: size) } } 
+1
source

Introduced with iOS 7, UIFontDescriptor is a tool for this.

To find out if the font is already bold, get the UIFontDescriptor of your font (via the UIFont fontDescriptor property), then call symbolicTraits and check the received bitmask for the UIFontDescriptorTraitBold .

Similarly, to find the bold version, take the font descriptor for the source font and call - fontDescriptorWithSymbolicTraits: You can then return it back to UIFont by calling + [UIFont fontWithDescriptor:size:] .

0
source

Source: https://habr.com/ru/post/1386677/


All Articles