我如何添加填充到的UILabel的内在含量的大小? [英] How can I add padding to the intrinsic content size of UILabel?

查看:150
本文介绍了我如何添加填充到的UILabel的内在含量的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自动版式上iOS7和我有这样一个问题:

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

我把一个UILabel到一个UIView,我安排我的自动布局的限制,使标签的的centerX =父视图的centerX。我不会放弃任何宽度约束的标签。当我设置标签的运行时间上的文字,将标签绘制一条窄窄的文字,以适应,没有利润/左侧和右侧垫。我想是对左,右两侧一些填充,使文本不会开始标签开始流向何方。黑客实现这一目标,可以设置文本作为@文本,但当然这不是要走的路:)

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 :)

如何才能实现我想要什么?

How can I achieve what I want?

推荐答案

您可以扩展的UILabel和自己覆盖intrinsicContentSize。请确保您已设置textAlignment = NSTextAlignmentCenter为好。

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

-(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.

这篇关于我如何添加填充到的UILabel的内在含量的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆