问题描述
我正在尝试使用 NSMutableAttributedString
为 UITextView
放置一个对齐的文本,NSMutableAttributedString
由不同的 NSAttributedString
因为我需要粗体和普通字体,所以我附加了不同的NSString
,这是我的NSMutableAttributedString
:
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];
我已经试过了:
[self.text_view setTextAlignment:NSTextAlignmentJustified]
还有这个:
NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment = NSTextAlignmentJustified;
Dictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
并将其应用于 NSMutableAttributedString,但两者都不起作用.我该怎么办?
and apply this to NSMutableAttributedString, but neither works. how i can do?
推荐答案
我通过为 NSAttributedString 指定透明背景色解决了同样的问题.
I fixed the same problem by specifying transparent background color for the NSAttributedString.
看起来像 UILabel 代码中的一个错误,它应该像 NSString 一样呈现简单的 NSAttributedString.
Looks like a bug in the UILabel code which should render simple NSAttributedString like NSString.
Xamarin.iOS 示例:
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;
这篇关于使用 UITextView 和 NSMutableAttributedString 对齐的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!