textView 的设置文本中某一文字的字体颜色以及图文混排
来源:互联网 发布:淘宝怎么注册卖家账户 编辑:程序博客网 时间:2024/05/01 12:28
ios7 新增的TextKit 技术 从书上看到的……
首先需要实例化3个对象
NSTextStorage //存储字符的相关属性 包括颜色等
NSLayoutManager //将字符变化反应到NstextContainer中
NSTextContainer //定义文本可以排版的区域
它们之间的关系是 [ NSLayoutManager addTextContainer: NSTextContainer]
[NSTextStorageaddLayoutManager:NSLayoutManager];
代码是
//将文字取出来 加到 NSTextStorage实例中
NSTextStorage *textStorage=[[NSTextStoragealloc]initWithString:self.textView.text];
//定义排版大小CGRect textViewRect=CGRectInset(self.view.bounds,10,20);
NSTextContainer* textContainer=[[NSTextContaineralloc]initWithSize:textViewRect.size];
//将排版大小加到 NSLayoutManager 对象中 它会将变化的字符串反应到文本上
NSLayoutManager *manager=[[NSLayoutManageralloc]init];
[manager addTextContainer:self.textContainer];
//将 NSLayoutManager 对象添加到NSTextStorage中
[textStorageaddLayoutManager:manager];
//实例化textView 并定义排版区域
self.textView =[[UITextViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,500)textContainer:self.textContainer];
self.textView.text=@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
[self.viewaddSubview:self.textView];
//设置文字效果设置效果必须放在这里之间 [textStorage beginEditing]--[textStorage endEditing]
[textStorage beginEditing];
//声明一个字体方式的字典
NSDictionary *attrsDic=@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
NSMutableAttributedString *assrStr=[[NSMutableAttributedStringalloc]initWithString:self.textView.textattributes:attrsDic];
[textStorage setAttributedString:assrStr];
//设置文字效果的方法 需要自己写
[self markWord:@"d"inTextStorage:textStorage];
[selfmarkWord:@"b"inTextStorage:textStorage];
[self markWord:@"c"inTextStorage:textStorage];
[textStorage endEditing];
-(void)markWord:(NSString *)word inTextStorage:(NSTextStorage *)textStorage
{
//设置某一文字效果
//正则判断是否存在word
NSRegularExpression *regex=[NSRegularExpressionregularExpressionWithPattern:wordoptions:0error:nil];
//存在word 就把存在的word 作为 NSTextCheckingResult 对象 存到数组中
NSArray *maches=[regexmatchesInString:self.textView.textoptions:0range:NSMakeRange(0,self.textView.text.length)];
for (NSTextCheckingResult *resultin maches) {
NSRange matchRange=[ result range];
//添加字体效果
[textStorage addAttribute:NSForegroundColorAttributeNamevalue:[UIColorredColor]range:matchRange];
}
}
以上是设置文字效果的 下面是设置图文混排的
首先要放置一张图片 把它放到textView 层上面 它们的父视图都是 self.view
[self.viewinsertSubview:self.textViewbelowSubview:self.ImageView];
//将图片放大一些
CGRect rect= CGRectMake(self.ImageView.frame.origin.x-10,self.ImageView.frame.origin.y-10,self.ImageView.frame.size.width+20,self.ImageView.frame.size.height+20);
CGRect imageRect=[self.textViewconvertRect:rectfromView:self.view];
//获取贝塞尔曲线 的四个点
UIBezierPath *bezierPath=[UIBezierPathbezierPathWithRect:imageRect];
//这句话 就是图文混排
self.textView.textContainer.exclusionPaths=@[bezierPath];
也是初学 写的不是很清楚 见谅希望有些评论
分别是
- textView 的设置文本中某一文字的字体颜色以及图文混排
- android TextView部分字体颜色设置以及图文混排
- TextView的图文混排,不同文字颜色混排
- 安卓textview设置文本中个别文字的颜色
- Android中TextView的字体颜色设置
- textView中设置多种颜色的文字
- Android TextView中文字通过SpannableString来设置不同的属性:超链接、颜色、字体等属性
- TextView的字体颜色设置
- andorid,动态设置TextView文本中字体的颜色和格式,用Html.fromHtml方法
- 设置TextView文字的颜色
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- MFC中设置静态文本的字体颜色、背景透明以及解决字体重叠
- TextView图文混排,设置部分字体背景色
- Android中实现不同文字颜色和图文混排的Span总结
- 使用ASP.NET实现Windows Service定时执行任务
- 挂载磁阵直到可以使用
- 高新技术(2)
- 字符串的全排列和组合算法
- 黑马程序员--java入门之环境变量图文教程
- textView 的设置文本中某一文字的字体颜色以及图文混排
- 解决遇到Linux网络配置,从熟悉网络配置文件入手
- ubuntu 上编译含有<math.h>的C源程序
- 【Python】Python学习(二)——Python程序如何运行
- RCP/SWT/JFACE 问题及解决方案
- Opencv step by step - 加载视频
- 矩阵转置
- CentOS设置开机自动启动某服务
- 【Android】Android Layout Binder——根据layout布局文件自动生成findViewById的java代码的神器