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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6544132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:35:40  来源:igfitidea点击:

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

iphoneiosfont-sizeuifont

提问by Mr. Ming

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

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

Or, if someone can implement this method for UIFont?

或者,是否有人可以为 UIFont 实现此方法?

- (UIFont *)boldFont;

采纳答案by Rafa? Augustyniak

UIFontDescriptoris pretty powerful class and can be used to get boldversion 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.

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

Here is the code in Swift 3:

这是 Swift 3 中的代码:

extension UIFont {

    func bold() -> UIFont? {
         let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
         let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
         return bondFontDescriptor.flatMap { UIFont(descriptor: 
@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
, size: pointSize) } } }

Here is the code in objective-c:

这是objective-c中的代码:

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

}

回答by EmilioPelaez

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

这是一个旧答案,它不再是最佳解决方案,请改为查看已接受的答案。

 @property(nonatomic, readonly) CGFloat pointSize

回答by Jhaliya

To access the font size from your UIFont instance use

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

capHeight,xHeight,pointSize 

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

有更多的属性来告诉字体大小(大写,小等......)

UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];

Use below to access the bold font

使用下面的访问粗体字体

UIFont *anyFont;
//initialization and other stuff here

回答by Ali Asad

Here is the easiest way to find font size

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

 CGFloat fontSize= [anyFont pointSize];

Now at some point you want to find its size

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

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)
    }

}

回答by titaniumdecoy

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

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

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.

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

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

编辑:我可能误解了你的问题。也许你正在寻找的pointsizeUIFont的财产?

回答by Marty

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

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

回答by RolandasR

Swift 3 version of rafa?-augustyniakanswer

Swift 3 版本的rafa?-augustyniak答案

##代码##