【问题标题】:Change just Font of AttributedText in Swift在 Swift 中仅更改 AttributedText 的字体
【发布时间】:2017-07-08 03:58:30
【问题描述】:

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

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

我进行了研究,但找不到实现此目的的直接方法。有什么想法吗?

【问题讨论】:

标签: ios swift nsattributedstring uifont


【解决方案1】:

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

  • Helvetica 是一个家庭
  • Helvetica BoldHelvetica ItalicHelvetica Bold ItalicHelvetica Display 等是面孔
  • Helvetica Bold, 12pt 是一个字体

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

斯威夫特 4

// 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 文本):

【讨论】:

    【解决方案2】:

    上面的效果很好,但是在 Swift4 和 Xcode 9.1 中,我收到了很多方法名称已更改的警告。下面是应用所有这些警告的结果。否则我什么都没做。

    let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText!)
    
    // Enumerate through all the font ranges
    newAttributedString.enumerateAttribute(NSAttributedStringKey.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([UIFontDescriptor.AttributeName.family: "Hoefler Text"])
    
        // Ask the OS for an actual font that most closely matches the description above
        if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptor.AttributeName.family]).first {
            let newFont = UIFont(descriptor: newFontDescriptor, size: currentFont.pointSize)
            newAttributedString.addAttributes([NSAttributedStringKey.font: newFont], range: range)
        }
    }
    
    label.attributedText = newAttributedString
    

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2014-12-23
      • 2021-04-14
      • 2016-08-19
      • 2015-07-26
      相关资源
      最近更新 更多