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];



也是初学 写的不是很清楚 见谅希望有些评论

分别是

0 0