如何在UILabel的内在内容大小中添加填充?

时间:2022-02-28 08:11:11

I'm using autolayout on iOS7 and I have a problem like this:

我在iOS7上使用autolayout,我有这样的问题:

I'm putting a UILabel onto a UIView and I'm arranging my autolayout constraints so that the label's centerX = parent view's centerX. I'm not giving any width constraint to the label. When I set the label's text on runtime, the label is drawn just wide enough for the text to fit, there are no margins/paddings on the left and right sides. What I want is to have some padding on the left and right sides, so that the text doesn't begin just where the label begins. The hack to achieve this could be setting the text as @" text " but of course that's not the way to go :)

我正在将UILabel放到UIView上,我正在安排我的自动布局约束,以便标签的centerX =父视图的centerX。我没有给标签赋予任何宽度约束。当我在运行时设置标签的文本时,标签的绘制范围足够宽,以使文本适合,左侧和右侧没有边距/填充。我想要的是在左侧和右侧有一些填充,以便文本不会从标签开始的地方开始。实现这一目标的黑客可能是将文本设置为@“text”,但当然这不是要走的路:)

How can I achieve what I want?

我怎样才能实现我想要的目标?

4 个解决方案

#1


29  

You can extend UILabel and override the intrinsicContentSize by yourself. Please make sure you have set the textAlignment = NSTextAlignmentCenter as well.

您可以自己扩展UILabel并覆盖intrinsicContentSize。请确保您也设置了textAlignment = NSTextAlignmentCenter。

-(CGSize)intrinsicContentSize{
    CGSize contentSize = [super intrinsicContentSize];
    return CGSizeMake(contentSize.width + 50, contentSize.height);
}

This probably only works when you only have just one line of text to display.

这可能仅在您只显示一行文本时才有效。

#2


7  

You can create a UILabel subclass and override intrinsicContent,

您可以创建UILabel子类并覆盖intrinsicContent,

-(CGSize)intrinsicContentSize {
    CGSize s = [super intrinsicContentSize];
    s = CGSizeMake(s.width + 20, s.height);
    return s;
}

This will add a padding of 20 points to the width. If you want your text in the middle, be sure to set the text alignment to center.

这将在宽度上添加20点的填充。如果您希望文本位于中间,请务必将文本对齐方式设置为居中。

#3


4  

If you're using auto layout, you can set the horizontal constraints and use an NSDictionary in the metrics parameter to set this dynamically.

如果您正在使用自动布局,则可以设置水平约束并在metrics参数中使用NSDictionary来动态设置它。

For instance, if you wanted to give a 10pt padding to the inner content of a UIButton, you could do something like the following:

例如,如果要为UIButton的内部内容提供10pt填充,则可以执行以下操作:

NSDictionary *padding = @{ @"padding" : @(button.intrinsicContentSize.width + 20) };
NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button(==padding)]|" options:0 metrics:padding views:NSDictionaryOfVariableBindings(button)];

#4


1  

If you are trying to give the UILabel a different colour to its parent view, then you will need to enclose the UILabel in a UIView with the padding and the background colour you want. If your UILabels background colour is the same as its parent view, then I don't understand the problem just use auto layout to specify how much space you want relative to the thing it is next to.

如果您尝试为UILabel提供与其父视图不同的颜色,则需要将UILabel包含在UIView中,并使用填充和所需的背景颜色。如果您的UILabels背景颜色与其父视图相同,那么我不明白这个问题只是使用自动布局来指定相对于它旁边的东西需要多少空间。

#1


29  

You can extend UILabel and override the intrinsicContentSize by yourself. Please make sure you have set the textAlignment = NSTextAlignmentCenter as well.

您可以自己扩展UILabel并覆盖intrinsicContentSize。请确保您也设置了textAlignment = NSTextAlignmentCenter。

-(CGSize)intrinsicContentSize{
    CGSize contentSize = [super intrinsicContentSize];
    return CGSizeMake(contentSize.width + 50, contentSize.height);
}

This probably only works when you only have just one line of text to display.

这可能仅在您只显示一行文本时才有效。

#2


7  

You can create a UILabel subclass and override intrinsicContent,

您可以创建UILabel子类并覆盖intrinsicContent,

-(CGSize)intrinsicContentSize {
    CGSize s = [super intrinsicContentSize];
    s = CGSizeMake(s.width + 20, s.height);
    return s;
}

This will add a padding of 20 points to the width. If you want your text in the middle, be sure to set the text alignment to center.

这将在宽度上添加20点的填充。如果您希望文本位于中间,请务必将文本对齐方式设置为居中。

#3


4  

If you're using auto layout, you can set the horizontal constraints and use an NSDictionary in the metrics parameter to set this dynamically.

如果您正在使用自动布局,则可以设置水平约束并在metrics参数中使用NSDictionary来动态设置它。

For instance, if you wanted to give a 10pt padding to the inner content of a UIButton, you could do something like the following:

例如,如果要为UIButton的内部内容提供10pt填充,则可以执行以下操作:

NSDictionary *padding = @{ @"padding" : @(button.intrinsicContentSize.width + 20) };
NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[button(==padding)]|" options:0 metrics:padding views:NSDictionaryOfVariableBindings(button)];

#4


1  

If you are trying to give the UILabel a different colour to its parent view, then you will need to enclose the UILabel in a UIView with the padding and the background colour you want. If your UILabels background colour is the same as its parent view, then I don't understand the problem just use auto layout to specify how much space you want relative to the thing it is next to.

如果您尝试为UILabel提供与其父视图不同的颜色,则需要将UILabel包含在UIView中,并使用填充和所需的背景颜色。如果您的UILabels背景颜色与其父视图相同,那么我不明白这个问题只是使用自动布局来指定相对于它旁边的东西需要多少空间。