开发者

How to get the font-size or a bold-version of UIFont instance

How to get the font-size of a UIFont instance?

Or, if someone can implement this method for UIFont?

-开发者_开发技巧 (UIFont *)boldFont;


This is an old answer and it's no longer the best solution, please see the accepted answer instead.

-(UIFont *)boldFont{

//First get the name of the font (unnecessary, but used for clarity)
NSString *fontName = self.fontName;

//Some fonts having -Regular on their names. so we have to remove that before append -Bold / -BoldMT
fontName = [[fontName componentsSeparatedByString:@"-"] firstObject];

//Then append "-Bold" to it.
NSString *boldFontName = [fontName stringByAppendingString:@"-Bold"];

//Then see if it returns a valid font
UIFont *boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];

//If it's valid, return it
if(boldFont) return boldFont;

//Seems like in some cases, you have to append "-BoldMT"
boldFontName = [fontName stringByAppendingString:@"-BoldMT"];
boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];

//Here you can check if it was successful, if it wasn't, then you can throw an exception or something.
return boldFont;

}


To access the font size from your UIFont instance use

 @property(nonatomic, readonly) CGFloat pointSize

There are more property to tell the font size (for cap, small etc...)

capHeight,xHeight,pointSize 

Use below to access the bold font

UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];


UIFontDescriptor is pretty powerful class and can be used to get bold version of the font you want. It should be totally safe and future proof as it only requires usage of public API and doesn't depend on any string modifications.

Here is the code in Swift 3:

extension UIFont {

    func bold() -> UIFont? {
         let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
         let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
         return bondFontDescriptor.flatMap { UIFont(descriptor: $0, size: pointSize) }
    }

}

Here is the code in objective-c:

@implementation UIFont (RABoldFont)

- (UIFont *)ra_boldFont
{
    UIFontDescriptor *fontDescriptor = [self fontDescriptor];
    UIFontDescriptorSymbolicTraits traits = fontDescriptor.symbolicTraits;
    traits = traits | UIFontDescriptorTraitBold;
    UIFontDescriptor *boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:traits];
    return [UIFont fontWithDescriptor:boldFontDescriptor size:self.pointSize];
}

@end


Here is the easiest way to find font size

UIFont *anyFont;
//initialization and other stuff here

Now at some point you want to find its size

 CGFloat fontSize= [anyFont pointSize];


Use the NSString+UIKit methods to measure the size a string with a certain font.

The iPhone treats the normal and bold fonts of the same name as completely separate fonts, and as such there is no simple way to convert between them. For instance, ArialMT and Arial-BoldMT are considered completely different fonts by the OS.

EDIT: I may have misunderstood your question. Perhaps you are looking for the pointSize property of UIFont?


You can check for bold with Core Text. Use kCTFontBoldTrait.


Swift 3 version of rafał-augustyniak answer

extension UIFont {


    func boldFont() -> UIFont? {
        let fontDescriptor = self.fontDescriptor
        let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [ fontDescriptor.symbolicTraits, .traitBold ]

        guard let boldFontDesc = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits) else {
            return nil
        }

        return UIFont(descriptor: boldFontDesc, size: pointSize)
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜