有时候我们需要在文本中插入一些图片,但并不是使用 label imageView label 的形式,而是直接将图片插入到文本中显示。在iOS7.0,NSAttributedString中添加了一些新的功能可以帮助我们实现这一效果。
UITextView*textView = [[UITextView alloc]initWithFrame:CGRectMake(10,100,355,100)];
//textView.font = [UIFont systemFontOfSize:15];
textView.delegate = self;
[self.view addSubview:textView];
//textview里面插入表情
NSString*aaaa =@"这是一个表情[大笑],这个表情怎么样?";
NSRangerange = [aaaa rangeOfString:@"[大笑]"];
NSTextAttachment* emotiAtt = [[NSTextAttachment alloc]init];
textView.attributedText= [[NSAttributedString alloc]initWithString:[aaaa stringByReplacingOccurrencesOfString:@"[大笑]" withString:@""]];
emotiAtt.image= [UIImage imageNamed:@"Expression_14"];
[textView.textStorage insertAttributedString:[NSAttributedString attributedStringWithAttachment:emotiAtt] atIndex:range.location];
复制代码