2013-04-22 160 views
-1

下面是代码: -NSTextAlignmentJustified抛出错误

lblValue是的UILabel的一个实例。

[lblValue setText:@"Some Big String"]; 
[lblValue setTextAlignment: NSTextAlignmentJustified]; 

错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textAlignment does not accept NSTextAlignmentJustified'

+0

NSTextFieldAlignement只适用于iOS6的。如果您的目标是iOS5或iOS4.3,则无法使用。 – Larme 2013-04-22 10:23:08

回答

2

如果您正在使用iOS6的那么UITextAlignment已被弃用。您可以NSAttributedString或者您可以使用某些第三方归因的标签进行精细对齐以达到此目的。

UIKit的

NSString drawing additions and UILabel do not support NSTextAlignmentJustified orNSTextAlignmentNatural text alignments. Instead, use NSAttributedString drawing additions, which do support text alignments.

From UILabel Docs

If you are using styled text in iOS 6 or later, assigning a new value to this property causes the text alignment to be applied to the entirety of the string in the attributedText property. If you want to apply the alignment to only a portion of the text, create a new attributed string with the desired style information and associate it with the label. If you are not using styled text, this property applies to the entire text string in the text property.

+0

我正在使用NSTestAlignment而不是UITextAlignment – 2013-04-22 10:23:19

+0

您应该为此目的创建一个归因字符串来设置对齐。 – nsgulliver 2013-04-22 10:27:46

+0

你能告诉我一些示例代码来实现吗? – 2013-04-22 10:31:08