2017-02-18 83 views
2

AttributedText的只是字體我有一些在IB創建UILabels這一切都已歸於文本。每個標籤的文本都包含多行不同字體大小和顏色。變化斯威夫特

在運行時,我希望能夠在不更改現有字體大小或顏色的情況下更改這些標籤的字體名稱。

我已經研究過,無法找到一個簡單的方法來實現這一點。有任何想法嗎?

+0

的可能的複製[IOS SWIFT:是否有可能改變某個詞的字體樣式的字符串(http://stackoverflow.com/questions/29165560/ios-swift-is-it - 可以改變某種字符串的字體風格) – 2017-02-18 17:02:43

+0

和http://stackoverflow.com/questions/18365631/example-of-nsattributedstring-with-two -different-font-sizes – 2017-02-18 17:03:01

+0

@Sneak:這些問題似乎沒有解決保留原始屬性如字體顏色,字體大小等的核心問題。 – Kashif

回答

6

你首先需要了解蘋果公司用來描述字體的行話

  • Helvetica家庭
  • Helvetica BoldHelvetica ItalicHelvetica Bold ItalicHelvetica Display等都是面臨
  • Helvetica Bold, 12pt是一個字體

你需要的是更換一個屬性串的字體家族

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 

原件(舊金山):

San Francisco

更換(Hoefler文本):

Hoefler Text

-1

以上的偉大工程,但與Swift4和9.1的Xcode我得到這個方法的名字改變了警告的數量。以下是應用所有這些警告的結果。否則,我沒有改變任何東西。

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