2016-10-27 33 views
0

我需要一個UIButton多行。第一行將有一個FontAwesome的圖標,第二行有一個解釋圖標的單詞。如何使用FontAwesome創建多個行的UIButton?

此外,兩行的字體大小在每一行中必須不同。

這裏是我得到的時刻:

@IBOutlet weak var btnProfile: UIButton! 

let paraStyle = NSMutableParagraphStyle() 
paraStyle.lineBreakMode = NSLineBreakMode.byWordWrapping 
paraStyle.alignment = NSTextAlignment.center 

let icon = NSMutableAttributedString(string: "\u{f082}", attributes: [NSFontAttributeName: UIFont.init(name: "FontAwesome", size: 40)]) 
let text = NSMutableAttributedString(string:"\nProfile", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 12.0)]) 

icon.append(text) 
icon.addAttribute(NSParagraphStyleAttributeName, value: paraStyle, range: NSRange(location:0,length: icon.length)) 

btnProfile.setAttributedTitle(icon, for: .normal) 

,但我收到以下錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue renderingMode]: unrecognized selector sent to instance

我也使用與詢問符號方試圖內,而不是"\u{f082}"但問題是一樣的。

我知道問題出現在最後兩行,因爲如果我評論它們,應用程序不會引發任何異常。

而且我一直在使用故事板試了一下:

enter image description here

和它的作品幾乎良好。兩行都顯示爲圖標+文字,但文字具有圖標的字體和字體大小,我希望它們與衆不同。下面的截圖:

enter image description here

我在做什麼錯?我不在乎我是通過代碼還是故事板來解決這個問題。

在此先感謝!

+0

button.titleLabel .lineBreakMode = NSLineBreakMode.ByWordWrapping; button.titleLabel !.numberOfLines = 2 //如果你想讓無限數量的行放0,你可以設置標題圖標\ nyourText ... hope thi s幫助 – Joe

+0

@Joe我不想要無限數量的行。只是2.我試圖設置行數爲0,但它也不起作用,同樣的錯誤出現。 –

回答

1

試試這個代碼:

斯威夫特測試3

override func viewDidLoad() { 
    super.viewDidLoad() 

    //applying the line break mode 
    btnProfile?.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping; 

    let buttonText: NSString = "⭐️ Favourite\nProfile" 

    //getting the range to separate the button title strings 
    let newlineRange: NSRange = buttonText.range(of: "\n") 

    //getting both substrings 
    var substring1: NSString = "" 
    var substring2: NSString = "" 

    if(newlineRange.location != NSNotFound) { 
     substring1 = buttonText.substring(to: newlineRange.location) as NSString 
     substring2 = buttonText.substring(from: newlineRange.location) as NSString 
    } 


    //assigning diffrent fonts to both substrings 
    let font:UIFont? = UIFont(name: "Chalkduster", size: 50.0) 
    let attrString = NSMutableAttributedString(string: substring1 as String, attributes: NSDictionary(object: font!, forKey: NSFontAttributeName as NSCopying) as? [String : Any])   
    let font1:UIFont? = UIFont(name: "Noteworthy-Light", size: 30.0) 
    let attrString1 = NSMutableAttributedString(string: substring2 as String, attributes: NSDictionary(object: font1!, forKey: NSFontAttributeName as NSCopying) as? [String : Any]) 

    //appending both attributed strings 
    attrString.append(attrString1) 

    //assigning the resultant attributed strings to the button 
    btnProfile.setAttributedTitle(attrString, for: UIControlState.normal) 
    btnProfile.titleLabel?.textAlignment = .center 

} 

輸出:

enter image description here

+0

又如何在第一行和第二行添加不同的字體大小?我不希望他們使用相同的字體大小。 –

+0

我知道你會問this.let我看到...謝謝 – Joe

+0

我將更新我的代碼在一分鐘..感謝 – Joe

相關問題