ios Xcode:如何为标签文本添加边距/填充?

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

Xcode: How to add margin/padding to label text?

iosxcode

提问by bigpotato

I have a label with text:

我有一个带有文字的标签:

enter image description here

在此处输入图片说明

However, I want it have more padding (and for the edges to be a little rounded if possible). But when I stretch the label in my storyboard so that it appears to have padding, and then I restart the simulator, it doesn't add padding to it.

但是,我希望它有更多的填充(如果可能的话,边缘要圆一点)。但是,当我在故事板中拉伸标签以使其看起来有填充,然后我重新启动模拟器时,它不会向其中添加填充。

How would I do this? BTW, I'm using auto layout + objective c

我该怎么做?顺便说一句,我正在使用自动布局 + 目标 c

回答by the_critic

If you want to go the layout constraints route, this is probably the fitting solution for you:

如果您想走布局约束路线,这可能是适合您的解决方案:

You should use a container view and place the label inside of that. Then add constraints for the label, which will be equivalent to a padding.

您应该使用容器视图并将标签放在其中。然后为标签添加约束,这将等同于填充。

enter image description here

在此处输入图片说明

Select the label and click the constraints panel at the bottom right in the Storyboard file.

选择标签并单击 Storyboard 文件右下角的约束面板。

Label's constraints to container view

标签对容器视图的约束

With the label selected, apply layout constraints like seen above (make sure you select the red "lines" so that the constraints are actually applied).

选择标签后,应用如上所示的布局约束(确保选择红色“线”,以便实际应用约束)。

End result

最终结果

The orange view is the container view. The label is right inside of that.

橙色视图是容器视图。标签就在里面。

回答by Francisco Medina

You can add round corners from storyboard select your UILabel (or any view) and go to inspector, on the identity inspector section add a value as shown in the picture.

您可以从故事板添加圆角选择您的 UILabel(或任何视图)并转到检查器,在身份检查器部分添加如图所示的值。

enter image description here

在此处输入图片说明

回答by Dangerous Cat

To change the padding on a label with a background color where text is aligned left or right, first select the label then in the attributes inspector at the top and change text from Plain to Attributed in the dropdown menu

要更改具有文本左对齐或右对齐的背景颜色的标签上的填充,首先选择标签,然后在顶部的属性检查器中,并在下拉菜单中将文本从普通更改为属性

Select the alignment of the text (in this example it aligns right) click on the 3 dot menu button to the far right to open additional settings and change Text-Direction from Natural to Right To Left

选择文本的对齐方式(在本例中为右对齐)单击最右侧的 3 点菜单按钮以打开其他设置并将文本方向从自然更改为从右到左

Text in label should now have a bit of padding on the right side

标签中的文本现在应该在右侧有一些填充

回答by Andrey

You need to override UILabel's drawRect method

你需要重写 UILabel 的 drawRect 方法

Sample code:

示例代码:

#import "OCPaddedLabel.h"
#define PAD 10.0f
#define PAD_VERT 6.0f

@implementation OCPaddedLabel

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

if you would like to add rounded corners, you need:

如果您想添加圆角,您需要:

label.layer.cornerRadius = 15.0f;
label.layer.masksToBounds = YES;

回答by Josué H.

You can add margin with

您可以添加边距

yourLabel.frame = CGRectMake(0,0,100,100)