简体   繁体   English

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

[英]How can I add padding to the intrinsic content size of UILabel?

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. 我正在将UILabel放到UIView上,我正在安排我的自动布局约束,以便标签的centerX =父视图的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 :) 实现这一目标的黑客可能是将文本设置为@“text”但当然这不是要走的路:)

How can I achieve what I want? 我怎样才能实现我的目标?

You can extend UILabel and override the intrinsicContentSize by yourself. 您可以自己扩展UILabel并覆盖intrinsicContentSize。 Please make sure you have set the textAlignment = NSTextAlignmentCenter as well. 请确保您也设置了textAlignment = NSTextAlignmentCenter。

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

Swift 5.0 Swift 5.0

open override var intrinsicContentSize: CGSize {
    let size = super.intrinsicContentSize
    return CGSize(width: size.width + 16, height: size.height)
}

This probably only works when you only have just one line of text to display. 这可能仅在您只显示一行文本时才有效。

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. 这将在宽度上添加20个点的填充。 If you want your text in the middle, be sure to set the text alignment to center. 如果您希望文本位于中间,请务必将文本对齐方式设置为居中。

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)];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加填充到内在内容大小不适用于UILabel - Add padding to Intrinsic Content Size not working in UILabel 如何向 UILabel 添加填充(和背景) - How can I add padding (and a background) to a UILabel iOS:如果我将UILabel添加到UIView,则视图是否具有标签的固有内容大小? - iOS: if I add UILabel to a UIView, will the view has intrinsic content size of the label? 有没有办法以编程方式让UILabel确定其固有内容的大小 - Is there a way to programmatically let a UILabel determine its intrinsic content size 如何让自动布局覆盖 UIImageView 的固有大小? - How can I get autolayout to override a UIImageView's intrinsic size? 如何使用AutoLayout使UIImage的高度与UILabel的固有高度相同? - How can I make my UIImage the same height as the intrinsic height of my UILabel using AutoLayout? 将UILabel作为子视图添加到具有编程约束的UITextView时,如何停止“内容大小”约束 - How can I stop the “content size” constraints when adding a UILabel as a subview to a UITextView, with programmatic constraints 我如何监听 UILabel 内容变化? - How can I listen for UILabel content changes? 如何在UIStackView IOS中向UILabel添加前导填充 - How to add leading padding to UILabel in UIStackView IOS 将填充添加到UILabel - Add Padding to UILabel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM