用TextKit实现表情混排

原文 : http://www.cocoachina.com/bbs/read.php?tid=144272&fpage=14

Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装,有了这个TextKit,以后不用再拿着CoreText来做累活了,根据苹果的说法,他们开发了两年多才完成,而且他们在开发时候也将表情混排作为一个使用案例进行研究,所以要实现表情混排将会非常容易。 TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的UILabel,UITextField,UITextView里面就可以使用了。

1.NSAtrributedString
这是所有TextKit的载体,所有的信息都会输入到NSAttributedString里面,然后将这个String输入到Text控件里面就可以显示了。


2.NSTextAttachment
iOS7新增的类,作为文本的附件,可以放文件,可以放数据,以 NSAttachmentAttributeName这个key放入NSAttributedString里面,在表情混排这里,我们将放入image。

3.重载NSTextAttachment
本来是可以直接使用NSTextAttachment,但是我们需要根据文字大小来改变表情图片的大小,于是我们需要重载NSTextAttachment,NSTextAttachment实现了NSTextAttachmentContainer,可以给我们改变返回的图像,图像的大小。
重载NSTextAttachment代码:

@interface MMTextAttachment : NSTextAttachment
{ 
} 
@end
@implementation MMTextAttachment 
//I want my emoticon has the same size with line's height 
- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0) 
{ 
return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height ); 
} 
@end

4.在你的代码里面加入:

1 NSMutableAttributedString * string = [[ NSMutableAttributedStringalloc ] initWithString:@"123456789101112" attributes:nil ] ; 
2 MMTextAttachment * textAttachment = [[ MMTextAttachment alloc ] initWithData:nil ofType:nil ] ; 
3 UIImage * smileImage = [ UIImage imageNamed:@"a.jpg" ] ; //my emoticon image named a.jpg 
4 textAttachment.image = smileImage ; 
5 NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ; 
6 [ string insertAttributedString:textAttachmentString atIndex:6 ] ; 
7 _textView.attributedText = string ;

最后,最厉害的一点是,从此以后,可以在输入的同时也可以编辑图片了。

  

posted @ 2015-03-20 17:45  Mr.Greg  阅读(289)  评论(0编辑  收藏  举报