ios - 在 swift 中使用 UILabel 的子类设置自定义边框

标签 ios swift xcode uilabel subclass

我为自定义底部边框创建了 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)

}

从属性检查器中,不要忘记像这样更改类。

enter image description here

输出:

enter image description here

关于ios - 在 swift 中使用 UILabel 的子类设置自定义边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44693017/

相关文章:

iphone - 实现 Testflight.com 和 Flurry.com 异常处理

ios - 用于室内导航的 map 编辑器

ios - 如何从 IOS 的启动屏幕检查 NsUserDefaults?

ios - 检查 session Cookie 的有效性

swift - 将 Internet url 加载到 Webview

xcode - 如何找到我可以导入的所有 swift 系统模块?

xcode - 我该如何解决这个问题 : "this class is not key value coding-compliant for the key imageview"?

ios - 定位不起作用

ios - Calendar.nextDate() 在使用 .backward 作为方向时表现得非常奇怪。仅限每月最后一天

iphone - 使用自定义对象对数组进行排序