一段文字中插入一张图片(lable,textview为例)

 NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:@"fdsahfjdsafkdhafjkhadjksfhjk" attributes:nil];

    

    NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nil ofType:nil];

    UIImage *img=[UIImage imageNamed:@"qrcode.png"];

    attachment.image=img;

    attachment.bounds=CGRectMake(0, -10, 20, 20);

    NSAttributedString *text=[NSAttributedString attributedStringWithAttachment:attachment];

    

    [str insertAttributedString:text atIndex:2];//index是位置

    self.textView.attributedText=str;

你可能感兴趣的:(iOS)