如何在UILabel周围画边界?

时间:2022-05-15 23:21:31

Is there a way for UILabel to draw a border around itself? This is useful for me to debug the text placement and to see the placement and how big the label actually is.

UILabel有没有办法在自己周围画一个边界?这对我调试文本位置和查看位置和标签的大小很有用。

9 个解决方案

#1


235  

You can set label's border via its underlying CALayer property:

您可以通过标签的底层CALayer属性设置标签的边框:

#import <QuartzCore/QuartzCore.h>

myLabel.layer.borderColor = [UIColor greenColor].CGColor;
myLabel.layer.borderWidth = 3.0;

#2


58  

Here are some things you can do with UILabel and its borders.

这里有一些你可以用UILabel和它的边界做的事情。

如何在UILabel周围画边界?

Here is the code for those labels:

以下是这些标签的代码:

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // label 1
        label1.layer.borderWidth = 1.0

        // label 2
        label2.layer.borderWidth = 5.0
        label2.layer.borderColor = UIColor.blue.cgColor

        // label 3
        label3.layer.borderWidth = 2.0
        label3.layer.cornerRadius = 8

        // label 4
        label4.backgroundColor = UIColor.cyan

        // label 5
        label5.backgroundColor = UIColor.red
        label5.layer.cornerRadius = 8
        label5.layer.masksToBounds = true

        // label 6
        label6.layer.borderWidth = 2.0
        label6.layer.cornerRadius = 8
        label6.backgroundColor = UIColor.yellow
        label6.layer.masksToBounds = true
    }
}

Note that in Swift there is no need to import QuartzCore.

注意,在Swift中,不需要导入QuartzCore。

See also

另请参阅

#3


15  

Swift version:

斯威夫特版本:

myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.greenColor().CGColor

For Swift 3:

为迅速3:

myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.green.cgColor

#4


2  

You can use this repo: GSBorderLabel

您可以使用这个repo: GSBorderLabel

It's quite simple:

很简单:

GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithTextColor:aColor
                                                     andBorderColor:anotherColor
                                                     andBorderWidth:2];

#5


1  

Swift 3/4 with @IBDesignable

迅速与@IBDesignable 3/4


While almost all the above solutions work fine but I would suggest an @IBDesignable custom class for this.

虽然几乎所有的解决方案都可以正常工作,但是我建议使用@IBDesignable自定义类。

@IBDesignable
class CustomLabel: UILabel {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }

    @IBInspectable var borderWidth: CGFloat = 2.0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.0 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }
}

#6


0  

it really depends on how many boarder use in your view , sometimes , just add a UIVIEW which the size is a bit bigger to create the border . the method is faster than produce a view

它取决于你的视图中有多少boarder的使用,有时候,只是添加一个UIVIEW它的大小比创建边框要大一些。该方法比生成视图要快

#7


0  

Using an NSAttributedString string for your labels attributedText is probably your best bet. Check out this example.

使用NSAttributedString字符串为您的标签指定文本可能是您最好的选择。看看这个例子。

#8


0  

UILabel properties borderColor,borderWidth,cornerRadius in Swift 4

UILabel属性的边界,边界宽度,快速4的角半径。

@IBOutlet weak var anyLabel: UILabel!
   override func viewDidLoad() {
        super.viewDidLoad()
        anyLabel.layer.borderColor = UIColor.black.cgColor
        anyLabel.layer.borderWidth = 2
        anyLabel.layer.cornerRadius = 5
        anyLabel.layer.masksToBounds = true
}

#9


0  

Solution for Swift 4:

迅速解决方案4:

yourLabel.layer.borderColor = UIColor.green.cgColor;

yourLabel.layer。borderColor = UIColor.green.cgColor;

#1


235  

You can set label's border via its underlying CALayer property:

您可以通过标签的底层CALayer属性设置标签的边框:

#import <QuartzCore/QuartzCore.h>

myLabel.layer.borderColor = [UIColor greenColor].CGColor;
myLabel.layer.borderWidth = 3.0;

#2


58  

Here are some things you can do with UILabel and its borders.

这里有一些你可以用UILabel和它的边界做的事情。

如何在UILabel周围画边界?

Here is the code for those labels:

以下是这些标签的代码:

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // label 1
        label1.layer.borderWidth = 1.0

        // label 2
        label2.layer.borderWidth = 5.0
        label2.layer.borderColor = UIColor.blue.cgColor

        // label 3
        label3.layer.borderWidth = 2.0
        label3.layer.cornerRadius = 8

        // label 4
        label4.backgroundColor = UIColor.cyan

        // label 5
        label5.backgroundColor = UIColor.red
        label5.layer.cornerRadius = 8
        label5.layer.masksToBounds = true

        // label 6
        label6.layer.borderWidth = 2.0
        label6.layer.cornerRadius = 8
        label6.backgroundColor = UIColor.yellow
        label6.layer.masksToBounds = true
    }
}

Note that in Swift there is no need to import QuartzCore.

注意,在Swift中,不需要导入QuartzCore。

See also

另请参阅

#3


15  

Swift version:

斯威夫特版本:

myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.greenColor().CGColor

For Swift 3:

为迅速3:

myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.green.cgColor

#4


2  

You can use this repo: GSBorderLabel

您可以使用这个repo: GSBorderLabel

It's quite simple:

很简单:

GSBorderLabel *myLabel = [[GSBorderLabel alloc] initWithTextColor:aColor
                                                     andBorderColor:anotherColor
                                                     andBorderWidth:2];

#5


1  

Swift 3/4 with @IBDesignable

迅速与@IBDesignable 3/4


While almost all the above solutions work fine but I would suggest an @IBDesignable custom class for this.

虽然几乎所有的解决方案都可以正常工作,但是我建议使用@IBDesignable自定义类。

@IBDesignable
class CustomLabel: UILabel {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }

    @IBInspectable var borderWidth: CGFloat = 2.0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }

    @IBInspectable var cornerRadius: CGFloat = 0.0 {
        didSet {
            layer.cornerRadius = cornerRadius
        }
    }
}

#6


0  

it really depends on how many boarder use in your view , sometimes , just add a UIVIEW which the size is a bit bigger to create the border . the method is faster than produce a view

它取决于你的视图中有多少boarder的使用,有时候,只是添加一个UIVIEW它的大小比创建边框要大一些。该方法比生成视图要快

#7


0  

Using an NSAttributedString string for your labels attributedText is probably your best bet. Check out this example.

使用NSAttributedString字符串为您的标签指定文本可能是您最好的选择。看看这个例子。

#8


0  

UILabel properties borderColor,borderWidth,cornerRadius in Swift 4

UILabel属性的边界,边界宽度,快速4的角半径。

@IBOutlet weak var anyLabel: UILabel!
   override func viewDidLoad() {
        super.viewDidLoad()
        anyLabel.layer.borderColor = UIColor.black.cgColor
        anyLabel.layer.borderWidth = 2
        anyLabel.layer.cornerRadius = 5
        anyLabel.layer.masksToBounds = true
}

#9


0  

Solution for Swift 4:

迅速解决方案4:

yourLabel.layer.borderColor = UIColor.green.cgColor;

yourLabel.layer。borderColor = UIColor.green.cgColor;