iOS 向 UILabel 添加左内边距

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

iOS Add left padding to UILabel

iosuilabel

提问by ssantos

I need to create a UILabelwith 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.

我需要创建一个UILabel带有背景颜色的,我想添加一些左填充,但我在这里找到的每个解决方案似乎都是一个令人讨厌的黑客。

Which is the 'standard' way to achieve this from iOS 5 ahead?

从 iOS 5 提前实现这一目标的“标准”方法是什么?

EDIT

编辑

A screenshot to illustrate my scenario.-

说明我的场景的屏幕截图。-

enter image description here

在此处输入图片说明

回答by siege_Perilous

For a full list of available solutions, see this answer: UILabel text margin

有关可用解决方案的完整列表,请参阅此答案:UILabel text margin



Try subclassing UILabel, like @Tommy Herbert suggests in the answer to [this question][1]. Copied and pasted for your convenience:

尝试对 UILabel 进行子类化,就像@Tommy Herbert 在 [this question][1] 的答案中建议的那样。为方便起见复制粘贴:

I solved this by subclassing UILabel and overriding drawTextInRect: like this:

我通过继承 UILabel 并覆盖 drawTextInRect: 解决了这个问题:

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {0, 5, 0, 5};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

回答by Mike

I know this is old, but for posterity..

我知道这是旧的,但为了后代..

The most important part is that you must override both intrinsicContentSize() and drawTextInRect() in order to account for AutoLayout

最重要的部分是您必须同时覆盖 internalContentSize() 和 drawTextInRect() 以考虑 AutoLayout

var contentInset: UIEdgeInsets = .zero {
    didSet {
        setNeedsDisplay()
    }
}

override public var intrinsicContentSize: CGSize {
    let size = super.intrinsicContentSize
    return CGSize(width: size.width + contentInset.left + contentInset.right, height: size.height + contentInset.top + contentInset.bottom)
}

override public func drawText(in rect: CGRect) {
    super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset))
}

回答by Daij-Djan

add a space character too the string. that's poor man's padding :)

在字符串中添加一个空格字符。那是穷人的填充物:)

OR

或者

I would go with a custom background view but if you don't want that, the space is the only other easy options I see...

我会使用自定义背景视图,但如果您不想要,空间是我看到的唯一其他简单选项......

OR write a custom label. render the text via coretext

或编写自定义标签。通过 coretext 渲染文本

回答by rudensm

#define PADDING 5

@interface MyLabel : UILabel

@end

@implementation MyLabel

- (void)drawTextInRect:(CGRect)rect {
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0, PADDING, 0, PADDING))];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
    return CGRectInset([self.attributedText boundingRectWithSize:CGSizeMake(999, 999)
                                                         options:NSStringDrawingUsesLineFragmentOrigin
                                                         context:nil], -PADDING, 0);
}

@end

回答by incmiko

UIView* bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 70)];
bg.backgroundColor = [UIColor blackColor];
UILabel* yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, yourWidth, yourHeight)];
[bg addSubview:yourLabel];

[self addSubview:bg];

回答by clearlight

Sometimes it's convenient to use UNICODE partial spaces to achieve alignment while prototyping. This can be handy in prototyping, proof-of-concept, or just to defer implementation of graphics algorithms.

有时在原型设计时使用 UNICODE 部分空格来实现对齐是很方便的。这在原型设计、概念验证或只是推迟图形算法的实现中非常有用。

If you use UNICODE spaces for convenience, be aware that at least one of the UNICODE spaces has a size based on the font it is displayed from, specifically the actual space character itself (U+0020, ASCII 32)

如果您为了方便而使用 UNICODE 空格,请注意至少一个 UNICODE 空格的大小取决于它显示的字体,特别是实际的空格字符本身(U+0020,ASCII 32)

If you're using the default iOS system font in a UILabel, the default System font characteristics could change in a subsequent iOS release and suddenly introduce an unwanted misalignment by changing your app's precise spacing. This can and does happen, for example the "San Francisco" font replaced a previous iOS system font in an iOS release.

如果您在 UILabel 中使用默认的 iOS 系统字体,默认的系统字体特征可能会在后续的 iOS 版本中更改,并通过更改应用程序的精确间距突然引入不必要的错位。这可能并且确实发生,例如“旧金山”字体替换了 iOS 版本中以前的 iOS 系统字体。

UNICODE easy to specify in Swift, for example:

在 Swift 中很容易指定 UNICODE,例如:

let six_per_em_space = "\u{2006}"

Alternatively, cut/paste the space from an HTML page directly into the UILabel's text field in Interface Builder.

或者,将 HTML 页面中的空间直接剪切/粘贴到界面生成器中 UILabel 的文本字段中。

Note:Attached pic is a screenshot, notHTML, so visit the linked pageif you want to cut/paste the space.

注意:附上的图片是截图,不是HTML,所以如果你想剪切/粘贴空间,请访问链接页面

UNICODE spaces

UNICODE 空间

回答by Ben

I had a couple of issues with the answers here, such as when you added in the padding, the width of the content was overflowing the box and that I wanted some corner radius. I solved this using the following subclass of UILabel:

我在这里的答案有几个问题,例如当您添加填充时,内容的宽度溢出了框,并且我想要一些圆角半径。我使用以下 UILabel 子类解决了这个问题:

#import "MyLabel.h"

#define PADDING 8.0
#define CORNER_RADIUS 4.0

@implementation MyLabel

- (void)drawRect:(CGRect)rect {
 self.layer.masksToBounds = YES;
 self.layer.cornerRadius = CORNER_RADIUS;
 UIEdgeInsets insets = {0, PADDING, 0, PADDING};
 return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
- (CGSize) intrinsicContentSize {
 CGSize intrinsicSuperViewContentSize = [super intrinsicContentSize] ;
 intrinsicSuperViewContentSize.width += PADDING * 2 ;
 return intrinsicSuperViewContentSize ;
}

@end

Hope that's helpful to someone! Note that if you wanted padding on the top and bottom, you would need to change this lines:

希望这对某人有帮助!请注意,如果您想在顶部和底部填充,则需要更改以下行:

UIEdgeInsets insets = {0, PADDING, 0, PADDING};

To this:

对此:

UIEdgeInsets insets = {PADDING, PADDING, PADDING, PADDING};

And add this line underneath the similar one for width:

并在类似的宽度下面添加这一行:

intrinsicSuperViewContentSize.height += PADDING * 2 ;

回答by Olcay Erta?

If you want to add padding to UILabelbut not want to subclass it you can put your label in a UIViewand give paddings with autolayout like:

如果您想添加填充UILabel但不想将其子类化,您可以将标签放入 aUIView并使用自动布局提供填充,例如:

Padding with UIView and autolayout

使用 UIView 和自动布局填充

Result:

结果:

Result

结果

回答by Ganesh Pawar

Swift 5

斯威夫特 5

Create below class file and set it to your label as custom class name through storyboard. That's it.

创建下面的类文件,并通过故事板将其设置为您的标签作为自定义类名。就是这样。

class PaddingLabel: UILabel {

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)//CGRect.inset(by:)
        super.drawText(in: rect.inset(by: insets))
    }
}

回答by Francesco D.M.

One thing I did to overcome this issue was to use a UIButton instead of a UILabel. Then in the Attributes Inspector of the Interface Builder, I used the Edge for the Title as the padding.
If you do not attach the button to an action, when clicked it will not get selected but it will still show the highlight.

我为克服这个问题所做的一件事是使用 UIButton 而不是 UILabel。然后在界面生成器的属性检查器中,我使用标题的边缘作为填充。
如果您不将按钮附加到操作,则单击它时不会被选中,但仍会显示突出显示。

You can also do this programmatically with the following code:

您还可以使用以下代码以编程方式执行此操作:

UIButton *mButton = [[UIButton alloc] init];
[mButton setTitleEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
[mButton setTitle:@"Title" forState:UIControlStateNormal];
[self.view addSubView:mButton];

This approach gives the same result but sometimes it did not work for some reason that I did not investigate since if possible I use the Interface Builder.

这种方法给出了相同的结果,但有时由于我没有调查的某种原因它不起作用,因为如果可能我使用接口生成器。

This is still a workaround but it works quite nicely if the highlight doesn't bother you. Hope it is useful

这仍然是一种解决方法,但如果突出显示不打扰您,它会很好地工作。希望有用