ios 如何使用 Swift 圆化 UILabel 的边缘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31146242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to round edges of UILabel with Swift
提问by Michael Paniccia
I have looked in the attributes of labels in Xcode 6.3 but I have not found out how to round the edges in the same way that you can round the edges of a text field.
我已经查看了 Xcode 6.3 中标签的属性,但我还没有找到如何以与文本字段边缘圆角相同的方式来圆角边缘。
采纳答案by Tim
Assuming you have added a backgroundColor
to your label otherwise there would be no way to tell if it had edges, you can use QuartzCore to round the edges of a label.
假设您已将 a 添加backgroundColor
到标签,否则将无法判断它是否有边缘,您可以使用 QuartzCore 来圆化标签的边缘。
import QuartzCore
yourLabel.layer.backgroundColor = UIColor.redColor().CGColor
yourLabel.layer.cornerRadius = 5
yourLabel.layer.masksToBounds = true
回答by spencer.sm
The trick is to set maskToBounds
to true. Then your modifications to cornerRadius
will be visible.
诀窍是设置maskToBounds
为true。然后您的修改cornerRadius
将是可见的。
label.layer.masksToBounds = true
label.layer.cornerRadius = 5
回答by Mohammad Arifuzzaman
Use label layer corner radius to do that,
使用标签层角半径来做到这一点,
mylabel.layer.cornerRadius = yourvalue
if you don't want to show shadow then add,
如果你不想显示阴影然后添加,
mylabel.layer.masksToBounds = true
it worked for me fine.
它对我很好。
回答by Bera Bhavin
You can give cornerRadius
and border like this.
你可以cornerRadius
像这样给和边界。
For Swift 4 and 3
对于 Swift 4 和 3
MyLable.layer.masksToBounds = true
MyLable.layer.cornerRadius = 6
MyLable.layer.borderWidth = 2
MyLable.layer.borderColor = UIColor.black.cgColor
For Objective-C
对于 Objective-C
[MyLable.layer setMasksToBounds:TRUE];
[MyLable.layer setCornerRadius:6];
[MyLable.layer setBorderWidth:2];
[MyLable.layer setBorderColor:[UIColor blackColor].CGColor];
回答by Tommy
A way to round the corners of any CALayer
is to modify layer.cornerRadius
. By default that will affect only the background colour and any layer border you've applied. You can also enable clipsToBounds
to clip the pixel contents.
圆角任何一个的方法CALayer
是修改layer.cornerRadius
. 默认情况下,这只会影响背景颜色和您应用的任何图层边框。您还可以启用clipsToBounds
剪辑像素内容。
When a view, such as a UILabel
draws itself, it draws itself to the pixel contents of a layer. You can grab view.layer
to access the layer.
当一个视图(例如 aUILabel
绘制自身)时,它会将自身绘制到图层的像素内容。您可以通过抓取view.layer
来访问图层。
So you can set that layer's corner radius to affect the background colour of the view. You can also enable clipsToBounds
to crop any content within the view. Provided the view itself isn't drawing too close to the edge, that should achieve what you want. It should be correct for labels.
因此,您可以设置该图层的角半径以影响视图的背景颜色。您还可以启用clipsToBounds
裁剪视图中的任何内容。如果视图本身没有太靠近边缘,那应该可以实现您想要的。标签应该是正确的。
回答by Erik Nguyen
回答by Deepak Tagadiya
Swift:
迅速:
//round shape corner radius set
self.lblName.layer.masksToBounds = true
self.lblName.layer.cornerRadius = self.lblName.bounds.width / 2
//custom shape corner radius set
self.lblName.layer.masksToBounds = true
self.lblName.layer.cornerRadius = 12
回答by BHUVANESH MOHANKUMAR
Works fine in Xcode 8.1.2 with Swift 3, Tested during AUGUST 2017
在 Xcode 8.1.2 和 Swift 3 中工作正常,2017 年 8 月期间测试
"cornerRadius" is the key property to set the rounded edges, where if you are using the same style for all the labels in your application, I would recommend for an extension method.
“cornerRadius”是设置圆角边缘的关键属性,如果您对应用程序中的所有标签使用相同的样式,我建议使用扩展方法。
Code:
代码:
// extension Class
extension UILabel {
// extension user defined Method
func setRoundEdge() {
let myGreenColor = (UIColor(red: -0.108958, green: 0.714926, blue: 0.758113, alpha: 1.0))
//Width of border
self.layer.borderWidth = 1.0
//How much the edge to be rounded
self.layer.cornerRadius = 5.0
// following properties are optional
//color for border
self.layer.borderColor = myGreenColor.cgColor
//color for text
self.textColor = UIColor.red
// Mask the bound
self.layer.masksToBounds = true
//clip the pixel contents
self.clipsToBounds = true
}
}
Output:
输出:
Why Extension method?
为什么是扩展方法?
Create a Swift file and add the following code, which has the Extention method to the "UILabel" class, where this method is user defined but will work for all the label in your application and will help to maintain consistency and clean code, if you change any style in future require only in the extension method.
创建一个 Swift 文件并将以下代码添加到“UILabel”类中,其中包含 Extention 方法,该方法是用户定义的,但适用于应用程序中的所有标签,并有助于保持一致性和干净的代码,如果您将来更改任何样式只需要在扩展方法中。
回答by levin varghese
You create an extension for UIView with IBInspectable
您使用 IBInspectable 创建 UIView 的扩展
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}