Ios UIS标签swift的圆角
我正在以编程方式创建一个Ios UIS标签swift的圆角,ios,swift,uilabel,cornerradius,Ios,Swift,Uilabel,Cornerradius,我正在以编程方式创建一个UILabel。但是下面的代码没有给我圆角。我想我遗漏了一些非常基本的东西 var textLabel:UILabel? = UILabel() textLabel?.text = text textLabel?.frame = CGRect(x:point.x, y:point.y, width:(textLabel?.intrinsicContentSize.width)!, height:15) textLabel?.backgroundColor = UICol
UILabel
。但是下面的代码没有给我圆角。我想我遗漏了一些非常基本的东西
var textLabel:UILabel? = UILabel()
textLabel?.text = text
textLabel?.frame = CGRect(x:point.x, y:point.y, width:(textLabel?.intrinsicContentSize.width)!, height:15)
textLabel?.backgroundColor = UIColor.white
textLabel?.font = UIFont(name:"OpenSans", size:8)
textLabel?.sizeToFit()
textLabel?.layer.cornerRadius = 20.0
有人能告诉我正确的方向吗?为您的标签设置
masksToBounds
masksToBounds充当布尔值,指示是否将子层剪裁到层的边界
请参考apple。尝试以下方法:
yourLabel.layer.cornerRadius = 8.0
yourLabel.layer.masksToBounds = true
yourLabel.layer.borderColor = UIColor.white.cgColor
yourLabel.layer.borderWidth = 1.0
这将为您提供圆形边框
关键是属性“maskToBounds”,它是一个布尔值,指示是否将子层剪裁到层的边界。尝试以下操作:-
textLabel?.layer.cornerRadius = textLabel?.frame.size.height/2.0
textLabel?.layer.masksToBounds = true
如果要设置边框颜色,则:-
textLabel?.layer.borderColor = .red.cgColor
textLabel?.layer.borderWidth = 1.0
我认为你应该为textLabel设置maskToBounds。试试这个:
textLabel?.layer.masksToBounds = true
swift 4.2
设置标签角半径和工作精细度
labelVerified.layer.cornerRadius = 6
labelVerified.layer.masksToBounds = true
提问者已经提到了尺寸
layer.cornerRadius=20.0
,当你想要精确的转角时,可以使用它。如果“大小”大于“高度”,则无法以完美的形状调整大小。或者您可以根据需要使用常量值。可能重复
labelVerified.layer.cornerRadius = 6
labelVerified.layer.masksToBounds = true