在 Swift 中只更改 AttributedText 的字体 [英] Change just Font of AttributedText in Swift

查看:93
本文介绍了在 Swift 中只更改 AttributedText 的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 IB 中创建了许多 UILabel,它们都有属性文本.每个标签的文本包含多行不同字体大小和颜色.

I have a number of UILabels created in IB which all have attributed text. Each label's text contains multiple lines of different font sizes and colors.

在运行时,我希望能够仅更改这些标签的字体名称,而无需更改现有字体大小或颜色.

At run-time, I want to be able to change just the font name of these labels without changing the existing font sizes or colors.

我已经研究过,但找不到一种直接的方法来实现这一点.有什么想法吗?

I have researched and could not find a straight forward method to achieve this. Any ideas?

推荐答案

您首先需要了解 Apple 用来描述字体的行话:

You first need to understand the lingo Apple uses to describe a typeface:

  • Helvetica 是一个家族
  • Helvetica BoldHelvetica ItalicHelvetica Bold ItalicHelvetica Display 等都是 faces
  • Helvetica Bold, 12pt 是一种字体
  • Helvetica is a family
  • Helvetica Bold, Helvetica Italic, Helvetica Bold Italic, Helvetica Display etc. are faces
  • Helvetica Bold, 12pt is a font

您想要的是替换属性字符串的字体系列.

What you want is to replace the font family of an attributed string.

// Enumerate through all the font ranges
newAttributedString.enumerateAttribute(.font, in: NSMakeRange(0, newAttributedString.length), options: []) { value, range, stop in
    guard let currentFont = value as? UIFont else {
        return
    }

    // An NSFontDescriptor describes the attributes of a font: family name,
    // face name, point size, etc. Here we describe the replacement font as
    // coming from the "Hoefler Text" family
    let fontDescriptor = currentFont.fontDescriptor.addingAttributes([.family: "Hoefler Text"])

    // Ask the OS for an actual font that most closely matches the description above
    if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [.family]).first {
        let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
        newAttributedString.addAttributes([.font: newFont], range: range)
    }
}

label.attributedText = newAttributedString

斯威夫特 3

let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText)

// Enumerate through all the font ranges
newAttributedString.enumerateAttribute(NSFontAttributeName, in: NSMakeRange(0, newAttributedString.length), options: []) { value, range, stop in
    guard let currentFont = value as? UIFont else {
        return
    }

    // An NSFontDescriptor describes the attributes of a font: family name,
    // face name, point size, etc. Here we describe the replacement font as
    // coming from the "Hoefler Text" family
    let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptorFamilyAttribute: "Hoefler Text"])

    // Ask the OS for an actual font that most closely matches the description above
    if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptorFamilyAttribute]).first {
        let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
        newAttributedString.addAttributes([NSFontAttributeName: newFont], range: range)
    }
}

label.attributedText = newAttributedString

原始(旧金山):

替换(Hoefler 文本):

Replacement (Hoefler Text):

这篇关于在 Swift 中只更改 AttributedText 的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆