2016-03-04 32 views
0

我想改變應該顯示在UI中的標籤的字體。我用這個行:NSMutableAttributedString中可能的錯誤

let myAttribute = [ NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)! ] 
     let buttonTitleNSMut = NSMutableAttributedString(string: "\(buttonTitle)", attributes: myAttribute) 
     let buttonTitle:String = "\(buttonTitleNSMut)" 

     let button = createButtonWithTitle(buttonTitle as String) 

但我的輸出是不是在不同的字體的標籤,但一個長字符串的字體的所有的「規格」 ...... 我在做什麼錯?也許最後我說這是一個String的事實?

+0

你有沒有試過檢查或打印控制你使用的字符串'「\(buttonTitle)」'?它是否有你期望的文字? – Macondo2Seattle

+0

是的,問題不是'「\(buttonTitle)」' –

回答

0

嘗試下面的代碼片段,以瞭解您要實現的功能。

let plainText = "\(buttonTitle)" as NSString 
    let attributedText = NSMutableAttributedString(string: plainText as String) 

    attributedText.addAttribute(NSFontAttributeName: UIFont(name: "cirth1", size: 18.0)!) 
    button?.titleLabel?.attributedText = attributedText 

希望有所幫助。