gpt4 book ai didi

ios - 使用 UITextView 和 NSMutableAttributedString 对齐文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:17 25 4
gpt4 key购买 nike

我正在尝试使用 NSMutableAttributedStringUITextView 放置一个对齐的文本,NSMutableAttributedString 由不同的 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,但两者均无效。我该怎么办?

最佳答案

我通过为 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;

关于ios - 使用 UITextView 和 NSMutableAttributedString 对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com