【问题标题】:Setting custom border using sublass of UILabel in swift在swift中使用UILabel的子类设置自定义边框
【发布时间】:2025-12-04 08:20:08
【问题描述】:

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

    }
  }

在视图控制器中,我在@IBOutlet weak var someLabel:BottomBorderClass 上调用类

问题是没有显示边框和文本。请帮忙!!提前致谢。

【问题讨论】:

  • 您是否为 BottomBorderClass 使用过 IBDesignable ?
  • 如果您使用故事板:您是否在Identity Insprector 中设置了Custom Class
  • 检查我的答案。如果有帮助,请不要忘记单击向上箭头并检查符号。 :)
  • @niggeulimann 我只是忘记了那个确切的基本步骤。非常感谢。我将类设置为BottomBorder Class,它工作正常,::) 谢谢大家。

标签: ios swift xcode uilabel subclass


【解决方案1】:

你可以为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、...一起使用

【讨论】:

    【解决方案2】:

    像这样改变你的函数。

    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)
    
    }
    

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

    输出:

    【讨论】:

    • 谢谢伙计。我只是错过了一步,我已经在 cmets 部分进行了更新。
    最近更新 更多