我想在 UILabel 的任何给定文本前面添加一个空格。
我认为我可以按如下方式扩展 UILabel 类:
class UILabel_iKK: UILabel {
override var text: String? {
didSet {
if let txt = self.text {
self.text = " " + txt
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
但显然这会导致无限循环(即文本更改导致 didSet 一次又一次触发。
以最优雅的方式做到这一点的另一种方式是什么?
最佳答案
是的,这是可能的。尽管我不喜欢它,但以下代码不会永远循环:
didSet {
if let txt = self.text, txt.first != " " {
self.text = " " + txt
}
}
关于swift - 如何在 UILabel 文本中添加空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47908698/