天天看點

textView 的設定文本中某一文字的字型顔色以及圖文混排

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:word options: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:rect fromView:self.view];

    //擷取貝塞爾曲線 的四個點

   UIBezierPath *bezierPath=[UIBezierPathbezierPathWithRect:imageRect];

//這句話 就是圖文混排

    self.textView.textContainer.exclusionPaths=@[bezierPath];

也是初學 寫的不是很清楚 見諒希望有些評論

分别是

繼續閱讀