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