gpt4 book ai didi

iphone - UILabel 左右对齐

转载 作者:技术小花猫 更新时间:2023-10-29 11:14:48 25 4
gpt4 key购买 nike

我有 UILabel( cocoa touch 框架),我想左右对齐其文本。因此它会拉伸(stretch)里面的文本。

例子:就像我有这个文本“虽然节省了物理制造和运输的成本”它会像下面这样:

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

如你所见,左右对齐...

我怎样才能做到这一点???

非常感谢

  • 很抱歉我不得不用双引号来发布问题。

最佳答案

您应该使用我的 OHAttributedLabel 类。

它拥有显示 NSAttributedString 所需的一切,包括左对齐、居中、右对齐……和对齐,而且使用起来非常简单。

你可以找到它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 header 中定义的常量,它匹配相应的 CoreText 常量以对齐。Apple 的 SDK 中不存在此常量)


[编辑] iOS6 SDK

自 iOS6 SDK 起,UITextAlignmentJustify 不再工作并在运行时产生崩溃。现在,您应该设置 NSAttributedString 本身的文本对齐方式,而不是使用标签的 textAlignment 属性。

关于iphone - UILabel 左右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com