iOS向UILabel添加左边距 [英] iOS Add left padding to UILabel
问题描述
我需要创建一个带背景颜色的 UILabel
,我想添加一些左边填充,但我在这里找到的每个解决方案看起来都像是讨厌的黑客攻击。
I need to create a UILabel
with a background color, and I'd like to add some left padding but every solution I've found around here seems like a nasty hack.
从iOS 5开始实现这一目标的标准方式是什么?
Which is the 'standard' way to achieve this from iOS 5 ahead?
EDIT
用于说明我的方案的屏幕截图.-
A screenshot to illustrate my scenario.-
推荐答案
查看完整列表可用解决方案,请参阅以下答案: UILabel文字边距
For a full list of available solutions, see this answer: UILabel text margin
尝试继承UILabel,就像@Tommy Herbert在[this question] [1]的答案中所建议的那样。复制并粘贴以方便您使用:
Try subclassing UILabel, like @Tommy Herbert suggests in the answer to [this question][1]. Copied and pasted for your convenience:
我通过继承UILabel并覆盖drawTextInRect来解决这个问题:像这样:
I solved this by subclassing UILabel and overriding drawTextInRect: like this:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
这篇关于iOS向UILabel添加左边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!