Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在textview iOS中属性字符串的末尾添加nsstring_Ios_Objective C_Nsstring_Nsattributedstring - Fatal编程技术网

在textview iOS中属性字符串的末尾添加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

我有一个类似“你好”的文本,它将是一个红色的属性字符串。然后在该属性字符串的末尾,我想附加一个nsstring“Who is 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=[[NSMutableAttributedString alloc]initWithAttributedString:messageTextView.attributedText]
NSMutableAttributedString *newString = messageTextView.attibutedText;
[newString appendAttributedString: [[NSAttributedString alloc] initWithString: @"Who are you?"];
messageTextView.attibutedText = newString;