用TextKit实现图文混排(转载)

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.在你的代码里面加入:

 NSMutableAttributedString * string = [[ NSMutableAttributedStringalloc ] initWithString:@"123456789101112"  attributes:nil ] ;
    
    MMTextAttachment * textAttachment = [[ MMTextAttachment alloc ] initWithData:nil ofType:nil ] ;     UIImage * smileImage = [ UIImage imageNamed:@"a.jpg" ]  ;  //my emoticon image named a.jpg     textAttachment.image = smileImage ;     NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;     [ string insertAttributedString:textAttachmentString atIndex:6 ] ;          _textView.attributedText = string ;      最后,最厉害的一点是,从此以后,可以在输入的同时也可以编辑图片了,实例如下
用TextKit实现图文混排(转载)
上一篇:UVA1635 Irrelevant Elements(唯一分解定理 + 组合数递推)


下一篇:Windows server 2012 AD DS 搭建步骤