Answers:
将属性设置clipsToBounds
为true
addMessageLabel.clipsToBounds = true
我认为设置拐角半径的最佳方法是:
并确保选中“剪辑子视图”:
检查“剪辑子视图”等于该代码addMessageLabel.clipsToBounds = YES;
。
我的问题有点不同。
虽然我确实做了 btn.clipsToBounds = true
我没有打算这样做:
btn.layer.cornerRadius = 20
因为我的屏幕尺寸不同。相反,我遵循了这个答案并做了:
override func layoutSubviews() {
seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}
这没用,因为我忘了加super.layoutSubviews()
。正确的代码是:
override func layoutSubviews() {
super.layoutSubviews()
seeMoreButton.layer.cornerRadius = seeMoreButton.bounds.size.height / 2
}
我尝试了以下方法,并获得了成功的输出。
yourlabelname.layer.cornerRadius = 10.0f;
[yourlabelname setClipsToBounds:YES];
还有别的事阻止你吗?
clipsToBounds
默认设置为YES
,因此该行[yourlabelname setClipsToBounds:YES];
不在我的原始代码中。
//works perfect in Swift 2.0 for a circular or round image
@IBOutlet var theImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
//Make sure the width and height are same
self.theImage.layer.cornerRadius = self.theImage.frame.size.width / 2
self.theImage.layer.borderWidth = 2.0
self.theImage.layer.borderColor = UIColor.whiteColor().CGColor
self.theImage.clipsToBounds = true
}
将以下代码添加为UIView的扩展
//// Story board Extra Feature for create border radius, border width and border Color
extension UIView {
/// corner radius
@IBInspectable var borderColor: UIColor? {
set {
layer.borderColor = newValue!.cgColor
}
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
} else {
return nil
}
}
}
@IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
clipsToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
}
之后,您将在界面生成器本身中获得以下属性。