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

时间:2023-01-22 20:07:50

I have created a subclass of UILabel for custom bottom border. The subclass is: import UIKit

我已经为自定义底部边框创建了UILabel的子类。子类是:import UIKit

class BottomBorderClass: UILabel {

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)

}

}

}

In view controller i am calling the class on @IBOutlet weak var someLabel:BottomBorderClass

在视图控制器中,我在@IBOutlet弱变量some​​Label:BottomBorderClass上调用该类

The problem is the border and text is not getting displayed. Please help!! Thanks in Advance.

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

2 个解决方案

#1


1  

Change your function like this.

像这样改变你的功能。

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)

}

From your attribute inspector, don't forget to change class like this.

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

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

output:

输出:

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

#2


0  

You can create an extension for CALayer

您可以为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)
    }
}

and in your controller, for exemple :

在你的控制器中,例如:

@IBOutlet weak var someLabel: UILabel !
someLabel.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2)

with this method you can use addBorder function with any UILabel, UIButton, ...

使用此方法,您可以将addBorder函数与任何UILabel,UIButton,...一起使用

#1


1  

Change your function like this.

像这样改变你的功能。

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)

}

From your attribute inspector, don't forget to change class like this.

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

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

output:

输出:

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

#2


0  

You can create an extension for CALayer

您可以为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)
    }
}

and in your controller, for exemple :

在你的控制器中,例如:

@IBOutlet weak var someLabel: UILabel !
someLabel.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 2)

with this method you can use addBorder function with any UILabel, UIButton, ...

使用此方法,您可以将addBorder函数与任何UILabel,UIButton,...一起使用