[英]Add a nsstring at the end of attributed string in textview iOS
I have a text like " Hello There " which will be a attributed string with red color. 我有一个像“ Hello There ”这样的文本,它将是一个红色的属性字符串。 Then at the end of that attributed string I want to append a nsstring 'Who are you?'
然后在该属性字符串的末尾,我想附加一个nsstring'你是谁?' But whenever I append a nsstring, the whole string becomes normal nsstring and the property of the attributed string is being removed.
但每当我附加一个nsstring时,整个字符串就变成了普通的nsstring,并且删除了属性字符串的属性。 My attempt so far:
我到目前为止的尝试:
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;
What I did wrong? 我做错了什么? Please help me.
请帮我。
Replace the bottom lines 替换底线
NSString *messageWithContact = [NSString stringWithFormat: @"%@ %@", messageTextView.text, @"Who are you?"];
messageTextView.text=messageWithContact;
with 同
NSMutableAttributedString *newString = messageTextView.attibutedText;
[newString appendAttributedString: [[NSAttributedString alloc] initWithString: @"Who are you?"];
messageTextView.attibutedText = newString;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.