ios 如何从常规 UIFont 创建粗体 UIFont?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16015916/
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 23:11:18  来源:igfitidea点击:

How do I create a bold UIFont from a regular UIFont?

iosios5uikit

提问by Michael

If I have a UIFontobject, is it possible to convert it to bold? I don't know the font name, I just have a UIFont object. What I want is a function like

如果我有一个UIFont对象,是否可以将其转换为粗体?我不知道字体名称,我只有一个 UIFont 对象。我想要的是一个像

UIFont *boldFontFromFont(UIFont *input)
{
    return [input derivedFontWithFontWeight:UIFontWeightBold];
}

How can I change the code so that it works. (The code above does not work, I just made it up to illustrate the point.)

如何更改代码以使其正常工作。(上面的代码不起作用,我只是为了说明这一点而编的。)

Thanks in advance.

提前致谢。

采纳答案by filwag

To get a bold font you need to pass a specific name of the font from a font family. You can get a font family name from a given font, then list all fonts from this family. In general, a bold font will contain "bold" in its name, but the format isn't strict and there could be variations like "Helvetica-BoldOblique", for example. You can start from this code:

要获得粗体字体,您需要从字体系列中传递字体的特定名称。您可以从给定的字体中获取字体系列名称,然后列出该系列中的所有字体。通常,粗体字体的名称中会包含“bold”,但格式并不严格,并且可能存在诸如“Helvetica-BoldOblique”之类的变体。您可以从以下代码开始:

- (UIFont *)boldFontFromFont:(UIFont *)font
{
    NSString *familyName = [font familyName];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
    for (NSString *fontName in fontNames)
    {
        if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            UIFont *boldFont = [UIFont fontWithName:fontName size:font.pointSize];
            return boldFont;
        }
    }
    return nil;
}

回答by marcprux

iOS 7 introduces a new UIFontDescriptorclass, which makes it a lot easier:

iOS 7 引入了一个新的UIFontDescriptor类,这使它变得更容易:

UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:12];
NSLog(@"plain font: %@", font.fontName); // “HelveticaNeue”

UIFont *boldFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize];
NSLog(@"bold version: %@", boldFont.fontName); // “HelveticaNeue-Bold”

UIFont *italicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"italic version: %@", italicFont.fontName); // “HelveticaNeue-Italic”

UIFont *boldItalicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"bold & italic version: %@", boldItalicFont.fontName); // “HelveticaNeue-BoldItalic”

For people who got here looking for a Cocoa (macOS) equivalent, UIFontDescriptorcomes from NSFontDescriptor, available since 10.3.

对于到这里寻找 Cocoa (macOS) 等价物的人来说,UIFontDescriptor来自NSFontDescriptor10.3 后可用。

回答by David Hernandez

And if you are looking for the swift implementation:

如果您正在寻找快速实施

let normalFont = UIFont(name: "FONT_NAME", size: CGFloat(20))!
let boldFont = UIFont(descriptor: normalFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalFont.pointSize)

Hope this helps! Cheers!

希望这可以帮助!干杯!

回答by loungerdork

Attempting to derive the bold/italic font using font or family names no longer works correctly since iOS 7, due to the cryptic font family name of the system font. Below is a simple extension to derive the bold/italic font using the UIFontDescriptor class.

由于系统字体的神秘字体系列名称,从 iOS 7 开始尝试使用字体或系列名称派生粗体/斜体字体不再正常工作。下面是使用 UIFontDescriptor 类派生粗体/斜体字体的简单扩展。

+(UIFont *) font:(UIFont *)font bold:(BOOL)bold italic:(BOOL)italic
{
    NSUInteger traits = 0;
    if (bold)
    {
        traits |= UIFontDescriptorTraitBold;
    }
    if (italic)
    {
        traits |= UIFontDescriptorTraitItalic;
    }
    return [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:traits] size:font.pointSize];
}

回答by Joachim Deelen

This is a very old thread but someone may be interested in how to do this in Swift 5 nowadays.

这是一个非常古老的线程,但现在有人可能对如何在 Swift 5 中执行此操作感兴趣。

Easy like this:

像这样简单:

var font: UIFont = UIFont.systemFont(ofSize: 18)
if let newDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
    font = UIFont(descriptor: newDescriptor, size: font.pointSize)
}

回答by Nikita Kukushkin

Since this question pops up when you search for bold UIFonts in Swift, here's a fresh answer:

由于在Swift 中搜索粗体 UIFonts 时会弹出此问题,因此这里有一个新答案:

extension UIFont {
    /// Returns a new font in the same family with the given symbolic traits,
    /// or `nil` if none found in the system.
    func withSymbolicTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
        guard let descriptorWithTraits = fontDescriptor.withSymbolicTraits(traits)
            else { return nil }
        return UIFont(descriptor: descriptorWithTraits, size: 0)
    }
}

Example:

例子:

myFont.withSymbolicTraits(.taitBold) // returns the bold version of myFont

回答by user436818

You can either use

您可以使用

[UIFont boldSystemFontOfSize:12].

If you are using custom fonts you have to use the name directly

如果您使用自定义字体,则必须直接使用名称

[UIFont fontWithName:@"Helvetica-Bold" size:17.0].

You can look up the possible font names with

您可以使用以下命令查找可能的字体名称

[UIFont fontNamesForFamilyName:@"American Typewriter"].

In this Post: https://stackoverflow.com/a/15388946/436818Ben M has an idea how to get the bold version dynamically. But extend the method to be sure to get the bold version (if it exists) because there are other bold versions like CondensedBold too.

在这篇文章中:https: //stackoverflow.com/a/15388946/436818Ben M 有一个如何动态获取粗体版本的想法。但是扩展该方法以确保获得粗体版本(如果存在),因为还有其他粗体版本,例如 CondensedBold。