向UILabel添加左/右水平填充 [英] Add left/right horizontal padding to UILabel
问题描述
我需要创建一个具有背景色的UILabel
,我想添加一些左/右前/后水平填充.
I need to create a UILabel
with a background color, and I'd like to add some left/right leading/trailing horizontal padding.
但是我发现的每个解决方案似乎都是一个讨厌的黑客.
But every solution I've found seems like a nasty hack.
从iOS 5开始实现此目标的标准"方法是什么?
What is the 'standard' way to achieve this from iOS 5 forward?
用于说明我的情况的屏幕截图:
A screenshot to illustrate my scenario:
推荐答案
For a full list of available solutions, see this answer: UILabel text margin
尝试子类化UILabel,就像@Tommy Herbert在[此问题] [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)];
}
这篇关于向UILabel添加左/右水平填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!