3

UITextView我正在尝试为with放置一个合理的文本NSMutableAttributedStringNSMutableAttributedString由不同的文本组成,NSAttributedString因为我需要粗体和常规字体,所以我附加了不同NSString的,这是我的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];

我试过这个:

[self.text_view setTextAlignment:NSTextAlignmentJustified]

和这个:

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

并将其应用于 NSMutableAttributedString,但都不起作用。我该怎么办?

4

1 回答 1

1

我通过为 NSAttributedString 指定透明背景颜色解决了同样的问题。

看起来像 UILabel 代码中的一个错误,它应该像 NSString 一样呈现简单的 NSAttributedString。

Xamarin.iOS 示例:

var paragraphStyle = new NSMutableParagraphStyle();
paragraphStyle.Alignment = UITextAlignment.Justified;
var attributedText = new NSAttributedString(simpleString,
                         paragraphStyle: paragraphStyle,
                         backgroundColor: Color.Transparent.ToUIColor());
myLabel.AttributedText = attributedText;
于 2015-05-03T17:26:46.280 回答