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

标签 ios objective-c nsstring nsattributedstring

我有一个像“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/

相关文章:

ios - 获取表格 View 中点击的单元格(附件按钮)的位置

ios - 使用 Swift 2 + AFNetworking 发布图片

ios - rangeOfComposedCharacterSequencesForRange 将 0 个字符范围转换为 1 个字符范围

objective-c - Xcode 更新后应用程序崩溃

objective-c - ScriptingBridge 导致 iTunes 在退出后重新启动

iphone - 使用 NSKeyedArchiver 复制 UIView 抛出 NSInvalidUnarchiveOperationException

ios - Intel XDK 如何仅针对 iPhone 构建?

json - 使用逗号拆分 NSString

iphone - 正确地将 NSString 转换为 NSnumber

ios - 更改不同范围的字体时 NSMutableAttributedString 崩溃