Ana*_*and 4 iphone objective-c uilabel ios
我知道在iOS 3.0+中我可以使用
label.layer.cornerRadius = xxx;
Run Code Online (Sandbox Code Playgroud)
绕过UILabel的所有四个角(作为UIView子类),但我想只围绕标签的顶部两个角并使底角保持直角.
有什么办法可以用UILabel做到吗?其他解决方案假设自定义UIView,而不是UILabel.
dig*_*igo 28
以为我会发布包含BezierPath的完整代码.
CGRect bounds = label.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
对于Swift 4.0:
let bounds: CGRect = label.bounds
let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: ([.topLeft, .topRight]), cornerRadii: CGSize(width: 5.0, height: 5.0))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath
label.layer.mask = maskLayer
Run Code Online (Sandbox Code Playgroud)
wat*_*n12 16
您可以使用CALayers和mask进行此操作
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
其中maskPath是使用的UIBezierPath设置 bezierPathWithRoundedRect:byRoundingCorners:cornerRadii