在 swift 中使用 UILabel 的子类设置自定义边框
Setting custom border using sublass of UILabel in swift
我已经为自定义底部边框创建了 UILabel 的子class。 subclass 是:
导入 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)
}
}
在视图控制器中,我在@IBOutlet weak var someLabel:BottomBorderClass
上调用 class
问题是边框和文本没有显示。请帮忙!!提前致谢。
像这样更改您的函数。
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)
}
在您的属性检查器中,不要忘记像这样更改 class。
输出:
您可以为 CALayer 创建一个扩展
import Foundation
import UIKit
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.zero
border.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x: 0, y: self.bounds.height - thickness, width: self.bounds.width, height: thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.bounds.height)
break
case UIRectEdge.right:
border.frame = CGRect(x: self.bounds.width - thickness, y: 0, width: thickness, height: self.bounds.height)
break
default:
break
}
border.backgroundColor = color.cgColor;
self.addSublayer(border)
}
}
在你的控制器中,例如:
@IBOutlet weak var someLabel: UILabel !
someLabel.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2)
使用此方法,您可以将 addBorder 函数与任何 UILabel、UIButton 等一起使用...
我已经为自定义底部边框创建了 UILabel 的子class。 subclass 是: 导入 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)
}
}
在视图控制器中,我在@IBOutlet weak var someLabel:BottomBorderClass
上调用 class问题是边框和文本没有显示。请帮忙!!提前致谢。
像这样更改您的函数。
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)
}
在您的属性检查器中,不要忘记像这样更改 class。
输出:
您可以为 CALayer 创建一个扩展
import Foundation
import UIKit
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.zero
border.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x: 0, y: self.bounds.height - thickness, width: self.bounds.width, height: thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.bounds.height)
break
case UIRectEdge.right:
border.frame = CGRect(x: self.bounds.width - thickness, y: 0, width: thickness, height: self.bounds.height)
break
default:
break
}
border.backgroundColor = color.cgColor;
self.addSublayer(border)
}
}
在你的控制器中,例如:
@IBOutlet weak var someLabel: UILabel !
someLabel.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2)
使用此方法,您可以将 addBorder 函数与任何 UILabel、UIButton 等一起使用...