【问题标题】:Add a nsstring at the end of attributed string in textview iOS在 textview iOS 的属性字符串末尾添加一个 nsstring
【发布时间】:2015-03-04 11:35:33
【问题描述】:

我有一个像“Hello There”这样的文本,它将是一个带有红色的属性字符串。然后在该属性字符串的末尾,我想附加一个 nsstring '你是谁?但是每当我附加一个 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;

我做错了什么?请帮帮我。

【问题讨论】:

    标签: ios objective-c nsstring nsattributedstring


    【解决方案1】:

    替换底线

    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;
    

    【讨论】:

    • 编辑后的第一行应该是:NSMutableAttributedString *newString =[[NSMutableAttributedString alloc] initWithAttributedString:messageTextView.attributedText];
    猜你喜欢
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2021-02-14
    • 2011-09-16
    相关资源
    最近更新 更多