如何仅圆化 UILabel 的顶部两个角?

发布于 2024-12-29 11:24:05 字数 214 浏览 7 评论 0原文

我知道在 iOS 3.0+ 中我可以用来

 label.layer.cornerRadius = xxx;

圆化 UILabel(作为 UIView 子类)的所有四个角,但我只想圆化标签的顶部两个角并保持底部角为直角。

有什么办法可以用 UILabel 做到这一点吗?其他解决方案采用自定义 UIView,而不是 UILabel。

I know that in iOS 3.0+ I can use

 label.layer.cornerRadius = xxx;

to round all four corners of a UILabel (as a UIView subclass), but I want to round only the top two corners of the label and keep the bottom corners right-angled.

Is there any way I can do that with a UILabel? Other solutions assume a custom UIView, not a UILabel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别想她 2025-01-05 11:24:05

我想我会发布包含 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;

对于 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

Thought I would post the full code with the BezierPath included.

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;

For 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
苦笑流年记忆 2025-01-05 11:24:05

您可以使用 CALayers 和蒙版来执行此操作

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;

,其中 maskPath 是使用 bezierPathWithRoundedRect:byRoundingCorners:cornerRadii 设置的 UIBezierPath

You can do this using CALayers and masks

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
label.layer.mask = maskLayer;

where maskPath is a UIBezierPath set up using bezierPathWithRoundedRect:byRoundingCorners:cornerRadii

奈何桥上唱咆哮 2025-01-05 11:24:05

我已经检查了 UIView & 的类参考; UILabel 及其层,但无法通过 iOS 提供的方法找到任何方法来做到这一点。

您可以做一件事,创建一个 UIView &对其应用圆角。现在添加一个 UILabel 作为此 UIView 的子视图将其放置在底部 2 个圆角被标签覆盖的位置。这样你就可以得到你需要的效果了...

I have checked the Class Reference for UIView & UILabel and its layers, but could not find any way to do this with the means given by the iOS.

You could do one thing, create a UIView & apply rounded corners to it. Now add a UILabel as a subview to this UIView & position it in such a way so that the bottom 2 rounded corners are covered by the label. This way you get the effect you need...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文