【发布时间】:2017-04-11 00:02:25
【问题描述】:
我正在尝试通过子类化 UIButton 来制作多行按钮。为了避免绘制两个自定义 UILabel(我对 Swift/Xcode 还是很陌生),我将属性字符串用于现有的 UILabel,并使用换行符分割行,如下所示:
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 |
有没有办法在不使用两个自定义 UILabel 的情况下做到这一点?
谢谢
【问题讨论】:
标签: ios uibutton uilabel truncate