Ios 如何使用Swift使UILabel的边缘圆化
我已经在Xcode 6.3中查看了标签的属性,但是我还没有发现如何像对文本字段的边缘进行圆角一样对边缘进行圆角 假设您在标签上添加了Ios 如何使用Swift使UILabel的边缘圆化,ios,swift,Ios,Swift,我已经在Xcode 6.3中查看了标签的属性,但是我还没有发现如何像对文本字段的边缘进行圆角一样对边缘进行圆角 假设您在标签上添加了背景色,否则无法判断标签是否有边缘,您可以使用QuartzCore将标签边缘磨圆 import QuartzCore yourLabel.layer.backgroundColor = UIColor.redColor().CGColor yourLabel.layer.cornerRadius = 5 yourLabel.layer.masksToBounds
背景色
,否则无法判断标签是否有边缘,您可以使用QuartzCore将标签边缘磨圆
import QuartzCore
yourLabel.layer.backgroundColor = UIColor.redColor().CGColor
yourLabel.layer.cornerRadius = 5
yourLabel.layer.masksToBounds = true
使任何
CALayer
角变圆的一种方法是修改层。拐角半径
。默认情况下,这将仅影响背景颜色和应用的任何层边框。您还可以启用clipstobunds
来剪裁像素内容
当视图(如UILabel
)绘制自身时,它会将自身绘制到图层的像素内容。您可以抓取view.layer
来访问该层
因此,可以设置该层的角半径以影响视图的背景颜色。您还可以启用
clipstobunds
来裁剪视图中的任何内容。如果视图本身没有画得太靠近边缘,那么应该可以达到您想要的效果。对于标签来说应该是正确的。使用标签层角半径来实现这一点
mylabel.layer.cornerRadius = yourvalue
如果不想显示阴影,请添加
mylabel.layer.masksToBounds = true
这对我很管用 诀窍是将
maskToBounds
设置为true。然后您对cornerRadius
的修改将可见
label.layer.masksToBounds = true
label.layer.cornerRadius = 5
在Xcode 8.1.2和Swift 3中工作良好,2017年8月测试
“cornerRadius”是设置圆角的关键属性,如果应用程序中的所有标签使用相同的样式,我建议使用扩展方法
代码:
// 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
}
}
输出:
// 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
}
}
为什么使用扩展方法?
创建一个Swift文件并添加以下代码,该代码将扩展方法添加到“UILabel”类中,在该类中,此方法由用户定义,但适用于应用程序中的所有标签,并将有助于保持一致性和干净的代码(如果您将来更改任何样式,只需要在扩展方法中更改) 您可以这样给出
拐角半径
和边框
对于Swift 5、4和3
MyLable.layer.masksToBounds = true
MyLable.layer.cornerRadius = 6
MyLable.layer.borderWidth = 2
MyLable.layer.borderColor = UIColor.black.cgColor
目标C
[MyLable.layer setMasksToBounds:TRUE];
[MyLable.layer setCornerRadius:6];
[MyLable.layer setBorderWidth:2];
[MyLable.layer setBorderColor:[UIColor blackColor].CGColor];
您可以使用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
}
}
}
选择您的
UILabel
在inspector中,添加层.masksToBounds
和层.cornerRadius
要在类中处理的代码更少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
可能重复伟大的答案!基本上,这意味着您必须添加以下行:L.backgroundColor=UIColor.blackColor();L.layer.cornerRadius=5.0;L.clipsToBounds=true//L是您的UILabelWell不太可能-您绝对会将其子类化!请注意,这现在是Swift 3及更高版本中的
clipToBounds
。也可以在标签属性检查器的界面生成器(情节提要)中,在视图的绘图部分下启用clipToBounds
属性。@JamesHeald否,clipToBounds用于UIView,masksToBounds用于CALayer。“label.layer.masksToBounds=true“是一个重要的代码,如果您将角半径应用于标签,并且如果它不起作用,则添加此代码肯定会将角半径添加到特定标签。”。。所以这应该是正确的答案。。谢谢你@spencer.sm!!回答正确!yourLabel.layer.masksToBounds必须设置为true。这是一个在设计时不使用代码分配属性的好方法