ios 在iphone中设置标签边框

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

set label border in iphone

iosuilabelborder

提问by iOS_User

How can I set label's border which is dynamically generated (Not from Interface Builder)?

如何设置动态生成的标签边框(不是来自 Interface Builder)?

回答by Mihir Mehta

you can do it by

你可以这样做

Label.layer.borderColor = [UIColor whiteColor].CGColor;
Label.layer.borderWidth = 4.0;

before this you need to import a framework QuartzCore/QuartzCore.h

在此之前你需要导入一个框架 QuartzCore/QuartzCore.h

回答by Suragch

Swift version

迅捷版

enter image description here

在此处输入图片说明

Set label border

设置标签边框

label.layer.borderWidth = 2.0

Set border color

设置边框颜色

label.layer.borderColor = UIColor.blueColor().CGColor

Use rounded corners

使用圆角

label.layer.cornerRadius = 8

Make background color stay within rounded corners

使背景颜色保持在圆角内

label.layer.masksToBounds = true

回答by zonble

You can also try to subclass your label and override the drawRect: method to draw or a border or whatever you like:

您还可以尝试子类化您的标签并覆盖 drawRect: 方法以绘制或边框或您喜欢的任何内容:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] setStroke];
    CGContextStrokeRect(context, self.bounds);
}

回答by dannywartnaby

I'm not sure you can by default with UILabel. You might want to consider using a read-only (field.editing = NO) UITextFieldand setting it's borderStyle (which can be done programmatically using a UITextBorderStyle). That may be a little 'heavy' though. Another option may be to sub-class UILabelto draw your border.

我不确定默认情况下您可以使用UILabel. 您可能需要考虑使用只读 (field.editing = NO)UITextField并将其设置为 borderStyle (可以使用 a 以编程方式完成UITextBorderStyle)。不过,这可能有点“重”。另一种选择可能是子类UILabel来绘制您的边框。

Alternatively, and depending on your needs this may be better, use the backing CALayerand draw a border using it's borderColor and borderWidth properties.

或者,根据您的需要,这可能会更好,使用支持CALayer并使用其 borderColor 和 borderWidth 属性绘制边框。