如何仅圆化 UILabel 的顶部两个角?
我知道在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我会发布包含 BezierPath 的完整代码。
对于 Swift 4.0:
Thought I would post the full code with the BezierPath included.
For Swift 4.0:
您可以使用 CALayers 和蒙版来执行此操作
,其中 maskPath 是使用
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii
设置的 UIBezierPathYou can do this using CALayers and masks
where maskPath is a UIBezierPath set up using
bezierPathWithRoundedRect:byRoundingCorners:cornerRadii
我已经检查了 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 aUILabel
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...