用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];
//控制台显示: