图文混排(TextKit)
来源:互联网 发布:域名交易排行榜 编辑:程序博客网 时间:2024/05/17 02:01
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代码:
{ } @end @implementationMMTextAttachment //I want my emoticon has thesame size with line's height -(CGRect)attachmentBoundsForTextContainer:(NSTextContainer*)textContainer proposedLineFragment:(CGRect)lineFragglyphPosition:(CGPoint)positioncharacterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0) { return CGRectMake( 0 , 0 , lineFrag.size.height, lineFrag.size.height ); }
4.在你的代码里面加入:
@interface MMTextAttachment : NSTextAttachment
@end
NSMutableAttributedString
* string = [[
NSMutableAttributedStringalloc
] initWithString:@
"123456789101112"
attributes:
nil
] ;
MMTextAttachment* textAttachment = [[ MMTextAttachment alloc ]initWithData:
nil
ofType:
nil
] ;
UIImage* smileImage = [ UIImage imageNamed:@
"a.jpg"
] ;
//myemoticon image named a.jpg
textAttachment.image= smileImage ;
NSAttributedString
* textAttachmentString = [
NSAttributedString
attributedStringWithAttachment:textAttachment ] ;
[string insertAttributedString:textAttachmentString atIndex:6 ];
_textView.attributedText= string ;
1 0
- 图文混排(TextKit)
- 图文混排(TextKit详解)
- TextKit简单用法(图文混排)
- TextKit实现图文混排
- TextKit实现图文混排
- TextKit实现图文混排
- 用textkit实现图文混排
- textkit相关做图文混排键盘
- 富文本(图文混排)—— TextKit & CoreText
- 利用TextKit进行简单的图文混排
- 用TextKit实现表情混排
- 用TextKit实现表情混排
- SpannableString(图文混排)
- Android 图文混排(EditText)
- CoreText使用(2)图文混排
- CoreText(七):图文混排
- iOS 图文混排(swift)
- 图文混排
- malloc与calloc的区别
- JS中使用EL表达式
- PreferenceScreen 偏好显示类 的使用
- HashMap的内部结构及扩容
- java中抽象类跟接口的区别
- 图文混排(TextKit)
- poj 2566 Bound Found
- 行为型模式-观察者
- Android 中关于 【Cursor】 类的介绍
- 黑马程序员--数组
- Java 内存分配
- NSString 常用处理方法
- git clone 时,出现result=18,HTTP code=200,
- iframe获取焦点的问题、获取iframe src、打开新窗口window.open()