如何获取字体大小或UIFont实例的粗体版本

时间:2011-07-01 05:56:15

标签: iphone ios font-size uifont

如何获取UIFont实例的字体大小?

或者,如果有人可以为UIFont实现此方法?

- (UIFont *)boldFont;

7 个答案:

答案 0 :(得分:38)

这是一个陈旧的答案,它不再是最佳解决方案,请参阅接受的答案。

-(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;

}

答案 1 :(得分:25)

要从UIFont实例访问字体大小,请使用

 @property(nonatomic, readonly) CGFloat pointSize

有更多属性可以告诉字体大小(对于上限,小等等)

capHeight,xHeight,pointSize 

使用以下内容访问粗体字

UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];

答案 2 :(得分:15)

UIFontDescriptor是非常强大的类,可用于获取所需字体的粗体版本。它应该是完全安全的和未来的证明,因为它只需要使用公共API并且不依赖于任何字符串修改。

以下是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) }
    }

}

以下是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

答案 3 :(得分:8)

这是查找字体大小的最简单方法

UIFont *anyFont;
//initialization and other stuff here

现在在某些时候你想找到它的大小

 CGFloat fontSize= [anyFont pointSize];

答案 4 :(得分:2)

使用NSString+UIKit方法测量具有特定字体的字符串的大小。

iPhone将同名的普通和粗体字体视为完全独立的字体,因此没有简单的方法在它们之间进行转换。例如,ArialMT和Arial-BoldMT被OS认为是完全不同的字体。

编辑:我可能误解了你的问题。也许您正在寻找UIFont的pointSize属性?

答案 5 :(得分:0)

您可以使用Core Text检查粗体。使用kCTFontBoldTrait

答案 6 :(得分:0)

Swift 3版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)
    }

}