gpt4 book ai didi

ios - 在 TextView iOS 中的属性字符串末尾添加一个 nsstring

转载 作者:可可西里 更新时间:2023-11-01 06:22:57 25 4
gpt4 key购买 nike

我有一个像“Hello There”这样的文本,它将是一个带有红色的属性字符串。然后在该属性字符串的末尾我想附加一个 nsstring 'Who are you?'但是每当我附加一个 nsstring 时,整个字符串都会变成正常的 nsstring,并且属性字符串的属性将被删除。到目前为止我的尝试:

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello There"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,[attributeString length])];

NSMutableAttributedString *previousAttrString = [[NSMutableAttributedString alloc] initWithString:messageTextView.text];

[previousAttrString insertAttributedString: attributeString atIndex:location];
messageTextView.attributedText = previousAttrString;
messageTextView.font = [UIFont fontWithName:@"Helvetica" size:15];

NSString *messageWithContact = [NSString stringWithFormat: @"%@ %@", messageTextView.text, @"Who are you?"];
messageTextView.text=messageWithContact;

我做错了什么?请帮助我。

最佳答案

替换底线

NSString *messageWithContact = [NSString stringWithFormat: @"%@ %@", messageTextView.text, @"Who are you?"];
messageTextView.text=messageWithContact;

NSMutableAttributedString *newString = messageTextView.attibutedText;
[newString appendAttributedString: [[NSAttributedString alloc] initWithString: @"Who are you?"];
messageTextView.attibutedText = newString;

关于ios - 在 TextView iOS 中的属性字符串末尾添加一个 nsstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28853420/

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