ios 如何在iphone中使用圆角标签,UILabel圆角

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8530115/
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 15:54:56  来源:igfitidea点击:

How to use rounded corner label in iphone, UILabel Round Corners

iphoneios

提问by M.B

I am using some labels in a view. I want to make rounded corner label in my iphone application. I use following code to do this but it's not work. I got have some errors to use that properties.

我在视图中使用了一些标签。我想在我的 iphone 应用程序中制作圆角标签。我使用以下代码来做到这一点,但它不起作用。我在使用该属性时遇到了一些错误。

label.layer.borderColor = [UIColor blueColor].CGColor;
label.layer.borderWidth = 4.0;
label.layer.cornerRadius = 8;

采纳答案by XJones

Hard to know for sure what you're asking as you didn't include the errors you're getting. Have you added the QuartzCoreframework to your project and #import <QuartzCore/CALayer.h>to the file modifying the layer? If that's not it, add the errors and more info to your question.

很难确定你在问什么,因为你没有包括你得到的错误。您是否将QuartzCore框架添加到您的项目和#import <QuartzCore/CALayer.h>修改图层的文件中?如果不是这样,请将错误和更多信息添加到您的问题中。

EDIT: you can also #import <QuartzCore/QuartzCore.h>as suggested in the comments. QuartzCore.hincludes CALayer.halong with the rest of the QuartzCorecomponents.

编辑:您也可以#import <QuartzCore/QuartzCore.h>按照评论中的建议。QuartzCore.h包括CALayer.h与其余QuartzCore组件一起。

回答by Felipe FMMobile

I was facing the same problem, using a UILabel with backgroundColor in a cell, and added this to work correctly :

我遇到了同样的问题,在单元格中使用带有 backgroundColor 的 UILabel,并添加了它以正常工作:

label.layer.cornerRadius=8.0;
label.clipsToBounds=YES;

回答by Ajeet Pratap Maurya

Just add #import <QuartzCore/QuartzCore.h>in your .m file

只需添加#import <QuartzCore/QuartzCore.h>您的 .m 文件

and suppose you have a UILabel *myLabel;

假设你有一个 UILabel *myLabel;

just do [myLabel.layer setCornerRadius:20];//value '20' can be changed according to your wish :)

只是做[myLabel.layer setCornerRadius:20];//值'20'可以根据你的意愿改变:)

回答by Ramdhas

This simple code enough for RoundLabel

这个简单的代码对于RoundLabel 来说已经足够了

LabelName.layer.cornerRadius = LableName.frame.size.height/2;
LabelName.layer.masksToBounds = YES;

回答by daidai

I would create a view with rounded corners and add the label to that view.

我会创建一个带圆角的视图并将标签添加到该视图。