我为自定义底部边框创建了 UILabel 的子类。子类是: 导入 UIKit
class BottomBorderClass: UILabel {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.setBottomBorder()
}
override init(frame: CGRect) {
super.init(frame:frame)
self.setBottomBorder()
}
func setBottomBorder()
{
self.text = "TITLE LABEL"
self.textColor = UIColor.grayColor()
let layer:CALayer = self.layer
let bottomBorder:CALayer = CALayer.init(layer: layer)
bottomBorder.borderColor = UIColor.whiteColor().CGColor
bottomBorder.borderWidth = 2;
bottomBorder.frame = CGRectMake(-1, self.layer.frame.size.height-1,
self.layer.frame.size.width, 2);
bottomBorder.borderColor = UIColor.whiteColor().CGColor
self.layer.addSublayer(bottomBorder)
}
}
在 View Controller 中,我正在调用 @IBOutlet 上的类,weak var someLabel:BottomBorderClass
问题是边框和文本未显示。请帮忙!!提前致谢。
最佳答案
像这样改变你的函数。
func setBottomBorder(){
let borderWidth:CGFloat = 4.0 //Change this according to your needs
let lineView = UIView.init(frame: CGRect.init(x: 0, y:self.frame.size.height - borderWidth , width: self.frame.size.width, height: borderWidth))
lineView.backgroundColor = UIColor.green
self.addSubview(lineView)
}
从属性检查器中,不要忘记像这样更改类。
输出:
关于ios - 在 swift 中使用 UILabel 的子类设置自定义边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44693017/