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
set label border in iphone
提问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
回答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) UITextField
and 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 UILabel
to 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 CALayer
and draw a border using it's borderColor and borderWidth properties.
或者,根据您的需要,这可能会更好,使用支持CALayer
并使用其 borderColor 和 borderWidth 属性绘制边框。