简体   繁体   English

UILabel的Corner Radius属性在iOS 7.1中不起作用

[英]Corner Radius property of UILabel is not working in iOS 7.1

I am setting the cornerRadius property for UILabel . 我正在为UILabel设置cornerRadius属性。 Its working fine all version of iOS < 7.1 . 它的工作正常iOS < 7.1所有版本。 Following code i have used, 以下代码我用过,

UILabel *originaltagLbl = [[UILabel alloc]initWithFrame:CGRectMake(startX, startY, 100,30)];    
[originaltagLbl setFont:[UIFont fontWithName:@"MuseoSans-500" size:15]];
[originaltagLbl setTextAlignment:NSTextAlignmentCenter];
[originaltagLbl setTextColor:UIColorFromRGB(0xffffff)];
originaltagLbl.backgroundColor = [UIColor redColor];
originaltagLbl.layer.cornerRadius = 5;
originaltagLbl.layer.borderColor = [UIColor redColor].CGColor;
originaltagLbl.layer.borderWidth = 1;
[scrollView addSubview:originaltagLbl];

if i use this, just simply displaying the label as rectanglular box, So how to set the corner radius of UILabel in iOS 7.1 如果我使用它,只需将标签显示为矩形框,那么如何在iOS 7.1设置UILabel的圆角半径

Add the next line to your code: 在代码中添加下一行:

originaltagLbl.layer.masksToBounds = YES;

For information see this SO answer, or read documentation . 有关信息,请参阅 SO答案或阅读文档

Swift 3/4/5 斯威夫特3/4/5

    yourlabel.layer.cornerRadius = 8 //your desire radius
    yourlabel.layer.masksToBounds = true

尝试将UILabel的clipsToBounds属性设置为YES

It's true that clipsToBounds works in 7.1, but the problem is in situations where you're scrolling/animating it's really slow and makes everything laggy. ClipToBounds确实在7.1中工作,但问题是在你滚动/动画它的情况下它真的很慢并且使一切都变得迟钝。

Setting the background color on the layer instead of the uiview is all that is needed. 只需在图层而不是uiview上设置背景颜色即可。

See: UILabel layer cornerRadius negatively impacting performance 请参阅: UILabel图层cornerRadius对性能产生负面影响

You can use below code, 您可以使用以下代码,

[[myLabel layer] setCornerRadius:5.0f];
[[myLabel layer] setMasksToBounds:YES];

thanks, 谢谢,

Swift 2 solution : Swift 2解决方案:

 @IBOutlet weak var your_label: UILabel! your_label.layer.cornerRadius = 5 your_label.layer.masksToBounds = true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM