簡體   English   中英

如何圍繞UILabel的前兩個角?

[英]How do I round only the top two corners of a UILabel?

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

 label.layer.cornerRadius = xxx;

繞過UILabel的所有四個角(作為UIView子類),但我想只圍繞標簽的頂部兩個角並使底角保持直角。

有什么辦法可以用UILabel做到嗎? 其他解決方案假設自定義UIView,而不是UILabel。

以為我會發布包含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

您可以使用CALayers和mask進行此操作

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

其中maskPath是使用bezierPathWithRoundedRect設置的bezierPathWithRoundedRect:byRoundingCorners:cornerRadii

我已經檢查了UIView和UILabel及其圖層的類參考,但是無法通過iOS給出的方法找到任何方法。

你可以做一件事,創建一個UIView並應用圓角。 現在將UILabel作為子視圖添加到此UIView並以這樣的方式定位它,以便標簽覆蓋底部的2個圓角。 這樣你就可以獲得所需的效果......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM