我的代码用于证明标签文本的合理性如下: -
-(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;
}
我不知道为什么它不起作用。请给出最好的解决方案。
由于
答案 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;