在textview iOS中属性字符串的末尾添加nsstring
我有一个类似“你好”的文本,它将是一个红色的属性字符串。然后在该属性字符串的末尾,我想附加一个nsstring“Who is you?”,但每当我附加一个nsstring时,整个字符串都会变成普通的nsstring,并且属性字符串的属性会被删除。我迄今为止的努力:在textview iOS中属性字符串的末尾添加nsstring,ios,objective-c,nsstring,nsattributedstring,Ios,Objective C,Nsstring,Nsattributedstring,我有一个类似“你好”的文本,它将是一个红色的属性字符串。然后在该属性字符串的末尾,我想附加一个nsstring“Who is you?”,但每当我附加一个nsstring时,整个字符串都会变成普通的nsstring,并且属性字符串的属性会被删除。我迄今为止的努力: NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello There"]; [attr
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=[[NSMutableAttributedString alloc]initWithAttributedString:messageTextView.attributedText]代码>
NSMutableAttributedString *newString = messageTextView.attibutedText;
[newString appendAttributedString: [[NSAttributedString alloc] initWithString: @"Who are you?"];
messageTextView.attibutedText = newString;