用TextKit实现表情,图片,文字混排

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

用Xcode以前本身自带的coreText,coreImage,实现图文混排,代码量非常大,不容易理解,

而Textkit是iOS7新推出的类库,其实是在之前推出的CoreText上的封装, TextKit并没有新增的类,他是在原有的文本显示控件上的封装,可以使用平时我们最喜欢使用的 UILabel,UITextField,UITextView里面就可以使用了。

//传你需要处理的文本内容

NSString* str =@"nni-hao-textkit";

//创建NSMutableAttributedString

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

NSMutableAttributedString* attributed=[[NSMutableAttributedStringalloc]initWithString:str];

//给所有字符设置字体为

//给所有字符设置字体为Zapfino,字体为30

[attributed addAttribute:NSFontAttributeNamevalue: [UIFontfontWithName:@"Zapfino"size:30]

range:NSMakeRange(0, str.length)];

//分段控制,最开始4个字符颜色设置成黄色

[attributedaddAttribute:NSForegroundColorAttributeNamevalue: [UIColoryellowColor]range:NSMakeRange(0,4)];

//分段控制,第5个字符开始的3个字符,即第5、6、7字符设置为红色

[attributedaddAttribute:NSForegroundColorAttributeNamevalue: [UIColorredColor]range:NSMakeRange(4,3)];

//创建UITextView来显示

UITextView * lable = [[UITextView alloc] initWithFrame:self.view.bounds];

lable.textAlignment = UITextAlignmentCenter;

lable.attributedText = attributed;

[self.view addSubview:lable];

//控制台显示

//*********************************************************************************

//创建图片附件

NSTextAttachment*attach = [[NSTextAttachmentalloc]init];

attach.image= [UIImageimageNamed:@"3.jpg"];

attach.bounds=CGRectMake(0,70,50,50);

NSAttributedString* attachstring = [NSAttributedStringattributedStringWithAttachment:attach];

[attributedappendAttributedString:attachstring];

//控制台显示:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航