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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22348353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:42:24  来源:igfitidea点击:

Corner Radius property of UILabel is not working in iOS 7.1

iosiphoneobjective-cios7.1

提问by Surfer

I am setting the cornerRadiusproperty for UILabel. Its working fine all version of iOS < 7.1. Following code i have used,

我正在cornerRadiusUILabel. 它的所有版本的iOS < 7.1. 以下代码我使用过,

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 UILabelin iOS 7.1

如果我使用它,只需将标签显示为矩形框,那么如何设置UILabelin的角半径iOS 7.1

回答by iOS Dev

Add the next line to your code:

将下一行添加到您的代码中:

originaltagLbl.layer.masksToBounds = YES;

For information see thisSO answer, or read documentation.

有关信息,请参阅SO 答案,或阅读文档

回答by Shakeel Ahmed

Swift 3/4/5

迅捷 3/4/5

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

回答by nnarayann

Try setting the UILabel's clipsToBoundsproperty to YES

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

回答by Aaron Zinman

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.

clipsToBounds 在 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 对性能产生负面影响

回答by Kupendiran iOS

You can use below code,

您可以使用以下代码,

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

thanks,

谢谢,

回答by Fox5150

Swift 2 solution :

斯威夫特 2 解决方案:

@IBOutlet weak var your_label: UILabel!

your_label.layer.cornerRadius = 5
your_label.layer.masksToBounds = true