【IOS學習】CoreText學習筆記(二)設定文字屬性和插入圖片
阿新 • • 發佈:2019-02-10
設定文字和圖片的方法:
- 繪製文字的步驟是:設定NSAttributedString 或NSMutableAttributedString——> 通過attributedString 生成frameSetter ——> 生成CTFrame——>畫出來
- 設定文字屬性,重點是在NSMutableAttributedString的生成,對顏色、字型、字距進行設定。
- CoreText 中並沒有畫圖片的方法,我們同樣的在NSMutableAttributedString中插入一個空字元,然後設定這個空字元佔的位置和大小。把整個字串寫完以後。重點在這裡:解析生成的NSAttributedString,獲取到圖片應該畫的位置和大小,利用Core Graphics 把圖片畫到context中。
註釋全部寫在例子對應的位置上:
- (void)drawRect:(CGRect)rect { CGContextRef contxt = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(contxt, CGAffineTransformIdentity); CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.bounds.size.height); CGContextConcatCTM(contxt, flipVertical); NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"1.設定字形變換矩陣為CGAffineTransformIdentity,也就是說每一個字形都不做圖形變換,將當前context的座標系進行flip,2.為圖片設定CTRunDelegate,delegate決定留給圖片的空間大小。 3.把圖片畫上去" ]; [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, [attributedString length])]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0.0, 10)]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 20)]; //建立圖片位置 NSString *taobaoImageName = @"meile.png"; CTRunDelegateCallbacks imageCallbacks; imageCallbacks.version = kCTRunDelegateVersion1; imageCallbacks.dealloc = RunDelegateDeallocCallback; imageCallbacks.getAscent = RunDelegateGetAscentCallback; imageCallbacks.getDescent = RunDelegateGetDescentCallback; imageCallbacks.getWidth = RunDelegateGetWidthCallback; CTRunDelegateRef runDelegate = CTRunDelegateCreate(&imageCallbacks, (__bridge void *)taobaoImageName); NSMutableAttributedString *imageAttributedString =[[NSMutableAttributedString alloc]initWithString:@" "];//建立一個空格,來使attributedString生效 [imageAttributedString addAttribute:(NSString *)kCTRunDelegateAttributeName value:(__bridge id)runDelegate range:NSMakeRange(0, 1)];//設定回撥 CFRelease(runDelegate); [imageAttributedString addAttribute:@"imageName" value:taobaoImageName range:NSMakeRange(0, 1)]; [attributedString insertAttributedString:imageAttributedString atIndex:30];//把attribute 插入到一個特定的位置 //建立圖片位置至此完成那個 CTFramesetterRef ctFrameSetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attributedString); CGMutablePathRef path = CGPathCreateMutable(); CGRect bounds = CGRectMake(0.0, 20.0, self.bounds.size.width, self.bounds.size.height-20); CGPathAddRect(path, NULL, bounds); CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFrameSetter, CFRangeMake(0, 0), path, NULL); CTFrameDraw(ctFrame, contxt); //現在開始畫圖片 CFArrayRef lines = CTFrameGetLines(ctFrame); CGPoint lineOrigins [CFArrayGetCount(lines)]; //這是一個方法 CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), lineOrigins); for (int i = 0; i < CFArrayGetCount(lines); i++) { CTLineRef line = CFArrayGetValueAtIndex(lines, i); CGFloat lineAscent; CGFloat lineDescent; CGFloat lineLeading; CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, &lineLeading); CFArrayRef runs = CTLineGetGlyphRuns(line); for (int j = 0; j < CFArrayGetCount(runs); j++) { CGFloat runAscent; CGFloat runDescent; CGPoint lineOrigin = lineOrigins[i]; CTRunRef run = CFArrayGetValueAtIndex(runs, j); NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run); CGRect runRect; runRect.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0,0), &runAscent, &runDescent, NULL); runRect=CGRectMake(lineOrigin.x + CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL), lineOrigin.y - runDescent, runRect.size.width, runAscent + runDescent); NSString *imageName = [attributes objectForKey:@"imageName"]; //圖片渲染邏輯 if (imageName) { UIImage *image = [UIImage imageNamed:imageName]; if (image) { CGRect imageDrawRect; imageDrawRect.size = image.size; imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x; imageDrawRect.origin.y = lineOrigin.y +lineDescent+10;// 怎麼精確計算 CGContextDrawImage(contxt, imageDrawRect, image.CGImage); } } } } //畫圖片結束 CFRelease(ctFrame); CFRelease(path); CFRelease(ctFrameSetter); }
下的DemoCoreText