【发布时间】:2017-07-12 12:09:44
【问题描述】:
如何设置特定的圆角半径?我在单元格中添加了标签。标签是多行。
我尝试了以下代码,但问题是高度没有增加。
+ (UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius
{
if (tl || tr || bl || br) {
UIRectCorner corner = 0;
if (tl) corner = corner | UIRectCornerTopLeft;
if (tr) corner = corner | UIRectCornerTopRight;
if (bl) corner = corner | UIRectCornerBottomLeft;
if (br) corner = corner | UIRectCornerBottomRight;
UIView *roundedView = view;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = roundedView.bounds;
maskLayer.path = maskPath.CGPath;
roundedView.layer.mask = maskLayer;
return roundedView;
}
return view;
}
// In cellforRow code like following
[CommonUtils roundCornersOnView:cell.senderView onTopLeft:YES topRight:YES bottomLeft:YES bottomRight:NO radius:cell.senderView.frame.size.height / 2];
当我使用普通代码时,它的工作和高度增加。
cell.label.layer.cornerRadius = cell.label.frame.size.height/2;
请帮我解决这个问题。
【问题讨论】:
-
你的意思是Label的高度没有增加??
-
当我使用上层代码时,高度增加但标签没有增加。意味着尝试转角半径
-
好的。所以在设置正常圆角半径
cell.label.layer.cornerRadius = cell.label.frame.size.height/2;时它的工作方式??? -
是的.......它工作
-
不添加单独的UIView,,,你只需添加
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = roundedView.bounds; maskLayer.path = maskPath.CGPath; cell.label.layer.mask = maskLayer;
标签: ios objective-c iphone