Justified text with UITextView and NSMutableAttributedString

女生的网名这么多〃 提交于 2020-01-24 10:27:13

问题


I'm trying to put a justified text for a UITextView with NSMutableAttributedString, the NSMutableAttributedString is composed of different NSAttributedString because I need bold and regular font, so I append different NSString, this is my NSMutableAttributedString:

NSAttributedString *one = [[NSAttributedString alloc] initWithString:@"abc" 
                                                          attributes:boldDict];
NSAttributedString *two = [[NSAttributedString alloc] initWithString:@" def" 
                                                          attributes:regularDict];
NSAttributedString *three = [[NSAttributedString alloc] initWithString:@" ghi" 
                                                            attributes:boldDict];

NSMutableAttributedString *string = 
 [[NSMutableAttributedString alloc] initWithAttributedString:one];
[string appendAttributedString:two];
[string appendAttributedString:three];

I have tried this:

[self.text_view setTextAlignment:NSTextAlignmentJustified]

and this:

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified; 
Dictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};

and apply this to NSMutableAttributedString, but neither works. how i can do?


回答1:


I fixed the same problem by specifying transparent background color for the NSAttributedString.

Looks like a bug in the UILabel code which should render simple NSAttributedString like NSString.

Xamarin.iOS example:

var paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.Alignment = UITextAlignment.Justified;
var attributedText = new NSAttributedString(simpleString,
                         paragraphStyle: paragraphStyle,
                         backgroundColor: Color.Transparent.ToUIColor());
myLabel.AttributedText = attributedText;


来源:https://stackoverflow.com/questions/23430475/justified-text-with-uitextview-and-nsmutableattributedstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!