删除字符串中的"U0000fffc"数据 textView添加图片 以及添加后属性失效的解决

背景:在实现textView的富文本时,如果添加一张图片后,如果直接发送textView的内容时,图片会被字符串“U0000fffc”替换。

问题:如何删除“U0000fffc”字符串;如何替换textView中图片的字符串;textView添加完图片后,之前设置的textView的属性被初始化了,怎么办?

方法:

(1)对于第一个问题,百度到的答案如下:

NSString *codeString = @"uFFFC";
NSString *msg=[msg stringByReplacingOccurrencesOfString:codeString withString:@""];

 这个方法,试了之后,就可以的。

(2)基于textView的图片替换,一个个人觉得比较合适的方式如下:

因为textView添加图片时,用的是 NSTextAttachment 控件,所以,基于这个方法下,
/**
  将富文本转换为带有图片标志的纯文本
  sysmbol: 是图片的标志的字符串,例如:[图片]  
  attributeString: 富文本字符串
*/
- (NSString *)textStringWithSymbol:(NSString *)symbol attributeString:(NSAttributedString *)attributeString{
    NSString *string = attributeString.string;
    //最终纯文本
    NSMutableString *textString = [NSMutableString stringWithString:string];
    //替换下标的偏移量
    __block NSUInteger base = 0;
    
    //遍历
    [attributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, attributeString.length)
                                options:0
                             usingBlock:^(id value, NSRange range, BOOL *stop) {
                                 //检查类型是否是自定义NSTextAttachment类
                                 if (value && [value isKindOfClass:[NSTextAttachment class]]) {
                                     //替换
                                     [textString replaceCharactersInRange:NSMakeRange(range.location + base, range.length) withString:symbol];
                                     //增加偏移量
                                     base += (symbol.length - 1);
                                     //将富文本中最终确认的照片取出来
                                     NSTextAttachment *attachmentImg = (NSTextAttachment *)value;
                                     [self.mutImgArray addObject:attachmentImg.image];
                                 }
                             }];

    return textString;
}

 (3)textView添加完图片后,之前设置的textView的属性被初始化了。这个问题的意思是,如果textView的字体大小设置为18,颜色为紫色;添加完图片后,textView的字体大小为14,字体颜色为黑色(textView自带的效果);原因不详,解决方法是,设置完图片后,对textView的属性重新设置。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage * img = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self uploadPicture:img];
        NSTextAttachment* textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = img;
        textAttachment.bounds = CGRectMake(0, 0, self.view.frame.size.width - 28, [self getImgHeightWithImg:img]);
        NSAttributedString* imageAttachment = [NSAttributedString attributedStringWithAttachment:textAttachment];
        NSMutableAttributedString *attriStr = [_textView.attributedText mutableCopy];
        [attriStr appendAttributedString:imageAttachment];

        _textView.attributedText = attriStr;
        
        //-------对textView的属性重新设置
        _textView.textColor = [UIColor purpleColor];
        _textView.font = [UIFont systemFontOfSize:18];
    }];
}
原文地址:https://www.cnblogs.com/lyz0925/p/7273865.html