UILabel左右对齐 [英] UILabel justify left and right

查看:239
本文介绍了UILabel左右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UILabel(可可触摸框架),我想向右和左对齐其文本。
因此它会拉伸文本内部。

i have UILabel (cocoa touch framework) and i want to right and left justify its text. as a consequence it will stretch the text inside.

例如:
如果我有这个文本物理制造和运输,它将显示如下:

Example: like if i had this text "While the saved costs of physical manufacturing and shipping" it would appear like the following:

"While the saved"
"c o s t s   o f"
"p h y s i c a l"
 "manufacturing"
"a n d  shipping"

您可以看到左右对齐...

as you can see left and right justification...

如何实现?

很感谢


  • 对不起,我不得不把双重qitations发布问题。

推荐答案

您应该使用我的 OHAttributedLabel strong>

它具有显示 NSAttributedString 所需的一切,包括左对齐,

It has everything needed to display an NSAttributedString, including justifying left, center, right… and justified, and is really simple to use.

您可以找到 here on github

// suppose that label is an IBOutlet to an OHAttributedLabel (subclass oh UILabel)
label.textAlignment = UITextAlignmentJustify; // and that's all, OHAttributedLabel does everything needed for you!

(注意: UITextAlignmentJustify 在OHAttributedLabel标头中定义的常数,与匹配对应CoreText常数以进行对齐对齐。此常数在Apple的SDK中不存在)

(Note: UITextAlignmentJustify is a constant defined in OHAttributedLabel headers that matches corresponding CoreText constant for justify alignment. This constant does not exists in Apple's SDK)

自iOS6 SDK开始, UITextAlignmentJustify 在运行时崩溃。现在你应该设置 NSAttributedString 本身的文本对齐,而不是使用标签的 textAlignment 属性。

Since iOS6 SDK, the UITextAlignmentJustify does not work anymore and generate a crash at runtime. Now you should set the text alignment of your NSAttributedString itself instead of using the textAlignment property of the label.

这篇关于UILabel左右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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