我需要一個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}"
但問題是一樣的。
我知道問題出現在最後兩行,因爲如果我評論它們,應用程序不會引發任何異常。
而且我一直在使用故事板試了一下:
和它的作品幾乎良好。兩行都顯示爲圖標+文字,但文字具有圖標的字體和字體大小,我希望它們與衆不同。下面的截圖:
我在做什麼錯?我不在乎我是通過代碼還是故事板來解決這個問題。
在此先感謝!
button.titleLabel .lineBreakMode = NSLineBreakMode.ByWordWrapping; button.titleLabel !.numberOfLines = 2 //如果你想讓無限數量的行放0,你可以設置標題圖標\ nyourText ... hope thi s幫助 – Joe
@Joe我不想要無限數量的行。只是2.我試圖設置行數爲0,但它也不起作用,同樣的錯誤出現。 –