有没有办法只为UILabel的右上角和右下角设置cornerRadius?
我尝试了以下内容,但它根本无法正常工作,并且我没有使用下面的代码获得预期的输出。如果需要,我可以在我的代码中修改吗?
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:lblCollectPaymentAmount.bounds
byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = lblCollectPaymentAmount.bounds;
maskLayer.path = maskPath.CGPath;
lblCollectPaymentAmount.layer.mask = maskLayer;
lblCollectPaymentAmount.layer.masksToBounds = YES;
答案 0 :(得分:1)
@drashti:
它可能对你有帮助:
let rectShape = CAShapeLayer()
rectShape.bounds = self.customView.frame
rectShape.position = self.customView.center
rectShape.path = UIBezierPath(roundedRect: self.customView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.customView.layer.backgroundColor = UIColor.green.cgColor
//Here I'm masking the textView's layer with rectShape layer
self.customView.layer.mask = rectShape
答案 1 :(得分:0)
IOS 11中出现了maskedCorners
属性。
答案 2 :(得分:0)
Apple从iOS 11及更高版本引入了maskedCorners
属性,非常方便。
lbl.clipsToBounds = true
lbl.layer.cornerRadius = 10
lbl.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
//You can give an array for .layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner and .layerMaxXMaxYCorner to give all corner a radius.
您还可以为标签声明IBDesignable类。
@IBDesignable
class CornerLable: UILabel {
@IBInspectable var cornerRadius:CGFloat = 10
override func layoutSubviews() {
layer.cornerRadius = cornerRadius
layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner]
}
}
不要忘记在情节提要或代码中clipsToBounds = true
。
希望有帮助。