如何证明标签文字的合理性?

时间:2015-06-17 08:47:35

标签: ios objective-c iphone

我的代码用于证明标签文本的合理性如下: -

-(void)justifyToLabel:(UILabel*)label withStr:(NSString*)str{
    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
    paragraphStyles.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: str attributes: attributes];
    label.attributedText = attributedString;
}

我不知道为什么它不起作用。请给出最好的解决方案。

由于

1 个答案:

答案 0 :(得分:6)

请尝试以下代码:

您尚未为firstLineHeadIndent财产分配任何价值,这就是为什么它不起作用。

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
paragraphStyles.firstLineHeadIndent = 1.0;                //must have a value to make it work

NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];

label.attributedText = attributedString;