我试图通过传递字符串检查下面的方法将NSAttributed字符串设置为UILabel:
-(NSAttributedString *) returnRichTextForString:(NSString *) inputString {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[inputString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [attributedString length])];
return attributedString;
}
我给输入字符串:
@"<body> <p>This is some text.</p> <center>This text will be center-aligned.</center> <p>This is some other text. </p> </body>";
我希望<center>...</center>
标记内的文字与中心对齐。但它没有发生!除了CSS以外的任何方式?
无论如何,我不想将
NSTextAlignmentCenter
属性设置为 的UILabel。
提前致谢...