我想通過繼承UIButton做一個多行按鈕。爲了避免得出兩個自定義UILabel
(我還是很新的雨燕/ Xcode的),我使用的歸因字符串現有UILabel
和分割線與新線的性格,就像這樣:多行UIButton與每行獨立截尾
func prepareAttributedTitle(_ primaryTitle: String = "", _ secondaryTitle: String = "") {
let title = NSMutableAttributedString()
let first = NSAttributedString(string: primaryTitle, attributes: [
NSForegroundColorAttributeName: tintColor,
NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.systemFontSize, weight: UIFontWeightSemibold)
])
let newLine = NSAttributedString(string: "\n")
let second = NSAttributedString(string: secondaryTitle, attributes: [
NSForegroundColorAttributeName: tintColor.withAlphaComponent(0.75),
NSFontAttributeName: UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
])
title.append(first)
title.append(newLine)
title.append(second)
setAttributedTitle(title, for: .normal)
}
而結果是(很抱歉,我沒有足夠的代表,發佈圖片):
| This is the long first |
| line |
| Secondary line |
不過,我想獨立截斷線,像這樣:
| This is the long fi... |
| Secondary line |
有沒有辦法做到這一點,而不使用兩個自定義UILabels?
感謝