在UITextView中插入图片

来源:互联网 发布:网络的利与弊ppt图片 编辑:程序博客网 时间:2024/06/01 07:46

在UITextView中插入图片


 NSMutableAttributedString *string = [[NSMutableAttributedStringalloc] initWithAttributedString:self.textView.attributedText];

    

    NSTextAttachment *textAttachment = [[NSTextAttachmentalloc] initWithData:nilofType:nil] ;

    

    

    NSString *str = [[NSBundlemainBundle] pathForResource:@"001"ofType:@"png"];

    UIImage *img = [UIImageimageWithContentsOfFile:str];

    textAttachment.image = img;

    

    NSAttributedString *textAttachmentString = [NSAttributedStringattributedStringWithAttachment:textAttachment] ;

    

    [string insertAttributedString:textAttachmentStringatIndex:self.textView.text.length];//index为用户指定要插入图片在文本框中的位置,从0开始

    

    self.textView.attributedText = string;

0 0
原创粉丝点击