Core Text

Core Text实战

这里使用Core Text实现一个和之前NSTextView显示类似的图文混排的例子。

直接贴上代码大家体会下:

[html]  view plain copy
  1. void RunDelegateDeallocCallback( void* refCon ){  
  2.       
  3. }  
  4.   
  5. CGFloat RunDelegateGetAscentCallback( void *refCon ){  
  6.     NSString *imageName = (NSString *)refCon;  
  7.     return [UIImage imageNamed:imageName].size.height;  
  8. }  
  9.   
  10. CGFloat RunDelegateGetDescentCallback(void *refCon){  
  11.     return 0;  
  12. }  
  13.   
  14. CGFloat RunDelegateGetWidthCallback(void *refCon){  
  15.     NSString *imageName = (NSString *)refCon;  
  16.     return [UIImage imageNamed:imageName].size.width;  
  17. }  
  18.   
  19.   
  20. - (void)drawRect:(CGRect)rect  
  21. {  
  22.     CGContextRef context = UIGraphicsGetCurrentContext();  
  23.       
  24.     //这四行代码只是简单测试drawRect中context的坐标系  
  25.     CGContextSetRGBFillColor (context, 1, 0, 0, 1);  
  26.     CGContextFillRect (context, CGRectMake (0, 200, 200, 100 ));  
  27.     CGContextSetRGBFillColor (context, 0, 0, 1, .5);  
  28.     CGContextFillRect (context, CGRectMake (0, 200, 100, 200));  
  29.       
  30.     CGContextSetTextMatrix(context, CGAffineTransformIdentity);//设置字形变换矩阵为CGAffineTransformIdentity,也就是说每一个字形都不做图形变换  
  31.       
  32.     CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,self.bounds.size.height);  
  33.     CGContextConcatCTM(context, flipVertical);//将当前context的坐标系进行flip  
  34.       
  35.     NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"测试富文本显示"] autorelease];  
  36.       
  37.     //为所有文本设置字体  
  38.     //[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, [attributedString length])]; // 6.0+  
  39.     UIFont *font = [UIFont systemFontOfSize:24];  
  40.     CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);  
  41.     [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)fontRef range:NSMakeRange(0, [attributedString length])];  
  42.       
  43.     //将“测试”两字字体颜色设置为蓝色  
  44.     //[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 2)]; //6.0+  
  45.     [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:NSMakeRange(0, 2)];  
  46.       
  47.     //将“富文本”三个字字体颜色设置为红色  
  48.     //[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)]; //6.0+  
  49.     [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(2, 3)];  
  50.       
  51.       
  52.     //为图片设置CTRunDelegate,delegate决定留给图片的空间大小  
  53.     NSString *taobaoImageName = @"taobao.png";  
  54.     CTRunDelegateCallbacks imageCallbacks;  
  55.     imageCallbacks.version = kCTRunDelegateVersion1;  
  56.     imageCallbacks.dealloc = RunDelegateDeallocCallback;  
  57.     imageCallbacks.getAscent = RunDelegateGetAscentCallback;  
  58.     imageCallbacks.getDescent = RunDelegateGetDescentCallback;  
  59.     imageCallbacks.getWidth = RunDelegateGetWidthCallback;  
  60.     CTRunDelegateRef runDelegate = CTRunDelegateCreate(&imageCallbacks, taobaoImageName);  
  61.     NSMutableAttributedString *imageAttributedString = [[NSMutableAttributedString alloc] initWithString:@" "];//空格用于给图片留位置  
  62.     [imageAttributedString addAttribute:(NSString *)kCTRunDelegateAttributeName value:(id)runDelegate range:NSMakeRange(0, 1)];  
  63.     CFRelease(runDelegate);  
  64.       
  65.     [imageAttributedString addAttribute:@"imageName" value:taobaoImageName range:NSMakeRange(0, 1)];  
  66.       
  67.     [attributedString insertAttributedString:imageAttributedString atIndex:1];  
  68.       
  69.     CTFramesetterRef ctFramesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attributedString);  
  70.       
  71.     CGMutablePathRef path = CGPathCreateMutable();  
  72.     CGRect bounds = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);  
  73.     CGPathAddRect(path, NULL, bounds);  
  74.       
  75.     CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFramesetter,CFRangeMake(0, 0), path, NULL);  
  76.     CTFrameDraw(ctFrame, context);  
  77.       
  78.     CFArrayRef lines = CTFrameGetLines(ctFrame);  
  79.     CGPoint lineOrigins[CFArrayGetCount(lines)];  
  80.     CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), lineOrigins);  
  81.       
  82.     for (int i = 0; i < CFArrayGetCount(lines); i++) {  
  83.         CTLineRef line = CFArrayGetValueAtIndex(lines, i);  
  84.         CGFloat lineAscent;  
  85.         CGFloat lineDescent;  
  86.         CGFloat lineLeading;  
  87.         CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, &lineLeading);  
  88.           
  89.         CFArrayRef runs = CTLineGetGlyphRuns(line);  
  90.         for (int j = 0; j < CFArrayGetCount(runs); j++) {  
  91.             CGFloat runAscent;  
  92.             CGFloat runDescent;  
  93.             CGPoint lineOrigin = lineOrigins[i];  
  94.             CTRunRef run = CFArrayGetValueAtIndex(runs, j);  
  95.             NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);  
  96.             CGRect runRect;  
  97.             runRect.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0,0), &runAscent, &runDescent, NULL);  
  98.               
  99.             runRect=CGRectMake(lineOrigin.x + CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL), lineOrigin.y - runDescent, runRect.size.width, runAscent + runDescent);  
  100.               
  101.             NSString *imageName = [attributes objectForKey:@"imageName"];  
  102.             //图片渲染逻辑  
  103.             if (imageName) {  
  104.                 UIImage *image = [UIImage imageNamed:imageName];  
  105.                 if (image) {  
  106.                     CGRect imageDrawRect;  
  107.                     imageDrawRect.size = image.size;  
  108.                     imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x;  
  109.                     imageDrawRect.origin.y = lineOrigin.y;  
  110.                     CGContextDrawImage(context, imageDrawRect, image.CGImage);  
  111.                 }  
  112.             }  
  113.         }  
  114.     }  
  115.       
  116.     CFRelease(ctFrame);  
  117.     CFRelease(path);  
  118.     CFRelease(ctFramesetter);  
  119. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值