swift - UILabel textInsets with attributesText
问题描述
我有以下子类,并希望添加功能以轻松更改行高、字符间距等。这需要使用 NSAttributedString 类来完成,但是每当我使用段落样式设置标签的属性文本时,文本插入在标签在顶部和底部被忽略。此实现在没有段落样式或“文本”属性的情况下工作......
class Label: UILabel {
override var text: String? {
get {
return super.attributedText?.string
}
set {
self.updateText(newValue)
}
}
public var textInsets: UIEdgeInsets = .zero {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = bounds.inset(by: textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
return textRect.inset(by: textInsets.invert())
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: textInsets))
}
private func updateText(_ text: String?) {
// Setting attributed text here...
}
}
我在具有自动高度的 UIStackView 中有标签。我可以看到标签周围有填充,但文本没有居中......每当我使用段落样式时。我需要我相信的行高倍数的段落样式。
任何帮助将非常感激!
解决方案
推荐阅读
- python - Python CSV:列内的逗号、单引号和双引号
- ruby-on-rails - SessionsController 中的 DropboxError - 创建
- html - 我在代码中使用 Bootstrap 4 轮播。Bootstrap 4 轮播不工作
- wordpress - Wordpress Plugin doesn't have the excepted behaviour
- django - 如何在 Django 的自定义帐户模型中覆盖 user.groups 以实现“虚拟”组?
- haskell - 简化代码(需要帮助细化这个问题的标题......)
- reactjs - 如何从数组中获取数据?无法从数组中获取 const 中的数据
- python - 获取多进程中使用的函数的返回值
- elixir - 在 Elixir 中迭代一个数组
- c - C printf改变函数内部变量的内容