问题描述
当我尝试使用我设计的自定义 UILabel 时,我遇到了一个奇怪的问题.该视图在故事板中可见,并且可以很好地处理属性.现在我所做的是在我的 Designable 类中我设置了一个名为 isError
的属性,当设置时,我需要在文本的开头附加一个 *.
I am facing a weird issue when I try to use the custom UILabel that I have designed. The view is visible in the storyboard and its working fine with properties. Now What I have done is in my Designable class i have set a property called isError
which when set, I need to append an * at the start of my text.
但是一旦在我的代码中我这样做了,我的可设计属性就没有被使用,标签也没有正确显示在设备上,它采用 UILabel 的默认属性,而不在文本中添加 *.不知道我哪里出错了.
But as soon as in my code I do that, My Designable properties are not used and the Label is not correctly shown on the device and it takes the default properties of UILabel without adding * to the text. Not sure where I am going wrong.
自定义标签代码
@IBDesignable class KGIBDesignableLabel: UILabel {
@IBInspectable var verticalPad: CGFloat = 0
@IBInspectable var horizontalPad: CGFloat = 0
var isError: Bool = false{
didSet {
setup()
}
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}
func setup(){
if isError{
text="*"+text!;
textColor = UIColor.KGColorPalette.errorMessageColor
}else{
textColor = UIColor.KGColorPalette.textEntryLabelColor
text=text!;
}
font = UIFont(name: "Helvetica", size: 14)
clipsToBounds = true
textAlignment = .center
numberOfLines = 0
lineBreakMode = NSLineBreakMode.byWordWrapping
sizeToFit()
}
override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize
let newWidth = superSize.width + superSize.height + (2 * horizontalPad)
let newHeight = superSize.height + (2 * verticalPad)
let newSize = CGSize(width: newWidth, height: newHeight)
return newSize
}
}
VC 中的访问代码
class ViewController: UIViewController {
@IBOutlet weak var labelCustom: KGIBDesignableLabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
labelCustom.isError=true
// After adding this^ line it takes default UILabel properties
}
推荐答案
First Mistake 在您的 layoutSubview
方法中调用 setup
导致连续调用从 setup
到 layoutSubview
因为您正在修改 setup
中导致 layoutSubview
被调用的内容
First Mistake calling setup
in your layoutSubview
method cause a consecutive calling from setup
to layoutSubview
because you are modifying things in the setup
that cause layoutSubview
is called
修复从 layoutSubviews()
第二个错误在您的 setup
中调用 sizeToFit()
将文本的大小调整为当前大小,然后再计算您的内在内容大小
Second Mistake calling sizeToFit()
in your setup
adjust the size of your text to the current size before calculate your intrinsic content size
修复从您的设置
第三个错误你设置为 width
label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad)
是很明显那里的高度是错误的
Third Mistake you were setting as width
the label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad)
is obvious that height is wrong there
Fix 将这一行 let newWidth = superSize.width + superSize.height + (2 * horizontalPad)
替换为这一行 `let newWidth = superSize.width + (2*horizontalPad)
Fix replace this line let newWidth = superSize.width + superSize.height + (2 * horizontalPad)
by this one `let newWidth = superSize.width + (2 * horizontalPad)
您的代码修改和工作
@IBDesignable class KGIBDesignableLabel: UILabel {
@IBInspectable var verticalPad: CGFloat = 0
@IBInspectable var horizontalPad: CGFloat = 0
var isError: Bool = false{
didSet {
setup()
}
}
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}
func setup(){
if isError{
text="*"+text!;
textColor = UIColor.red
}else{
textColor = UIColor.black
text=text!;
}
font = UIFont(name: "Helvetica", size: 14)
clipsToBounds = true
textAlignment = .center
numberOfLines = 0
lineBreakMode = NSLineBreakMode.byWordWrapping
}
override var intrinsicContentSize: CGSize {
let superSize = super.intrinsicContentSize
let newWidth = superSize.width + (2 * horizontalPad)
let newHeight = superSize.height + (2 * verticalPad)
let newSize = CGSize(width: newWidth, height: newHeight)
return newSize
}
}
这是使用 10 和 10 作为垂直和水平填充值的外观
this is how it looks with 10 and 10 as values por vertical and horizontal padding
希望能帮到你
这篇关于使用 IBDesignable UILabel 类时出现的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!