IOS CoreText.framework --- 图文混排

利用CORETEXT进行图文混排。

实现代码:

  1. void RunDelegateDeallocCallback( void* refCon ){  
  2.       
  3. }  
  4.   
  5. CGFloat RunDelegateGetAscentCallback( void *refCon ){  
  6.     NSString *imageName = (NSString *)refCon;  
  7.     return 80;//[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 100;//[UIImage imageNamed:imageName].size.width;  
  17. }  

先设置一个CTRun的委托,主要是用于指定对象的上行高,宽,或上下文释放时使用。
  1. -(void)drawCharAndPicture  
  2. {  
  3.     CGContextRef context = UIGraphicsGetCurrentContext();  
  4.       
  5.     CGContextSetTextMatrix(context, CGAffineTransformIdentity);//设置字形变换矩阵为CGAffineTransformIdentity,也就是说每一个字形都不做图形变换  
  6.       
  7.     CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,self.bounds.size.height);  
  8.     CGContextConcatCTM(context, flipVertical);//将当前context的坐标系进行flip  
  9.     NSLog(@"bh=%f",self.bounds.size.height);  
  10.       
  11.     NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"请在这里插入一张图片位置"] autorelease];  
  12.       
  13.       
  14.     //为图片设置CTRunDelegate,delegate决定留给图片的空间大小  
  15.     NSString *imgName = @"img.png";  
  16.     CTRunDelegateCallbacks imageCallbacks;  
  17.     imageCallbacks.version = kCTRunDelegateVersion1;  
  18.     imageCallbacks.dealloc = RunDelegateDeallocCallback;  
  19.     imageCallbacks.getAscent = RunDelegateGetAscentCallback;  
  20.     imageCallbacks.getDescent = RunDelegateGetDescentCallback;  
  21.     imageCallbacks.getWidth = RunDelegateGetWidthCallback;  
  22.     CTRunDelegateRef runDelegate = CTRunDelegateCreate(&imageCallbacks, imgName);  
  23.     NSMutableAttributedString *imageAttributedString = [[NSMutableAttributedString alloc] initWithString:@" "];//空格用于给图片留位置  
  24.     [imageAttributedString addAttribute:(NSString *)kCTRunDelegateAttributeName value:(id)runDelegate range:NSMakeRange(0, 1)];  
  25.     CFRelease(runDelegate);  
  26.       
  27.     [imageAttributedString addAttribute:@"imageName" value:imgName range:NSMakeRange(0, 1)];  
  28.       
  29.     [attributedString insertAttributedString:imageAttributedString atIndex:4];  
  30.       
  1.     //换行模式  
  2.     CTParagraphStyleSetting lineBreakMode;  
  3.     CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping;  
  4.     lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;  
  5.     lineBreakMode.value = &lineBreak;  
  6.     lineBreakMode.valueSize = sizeof(CTLineBreakMode);  
  7.       
  8.     CTParagraphStyleSetting settings[] = {  
  9.         lineBreakMode  
  10.     };  
  11.       
  12.     CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1);  
  13.       
  14.           
  15.     // build attributes  
  16.     NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)style forKey:(id)kCTParagraphStyleAttributeName ];  
  17.       
  18.     // set attributes to attributed string  
  19.     [attributedString addAttributes:attributes range:NSMakeRange(0, [attributedString length])];  
  20.       
  21.   
  22.       
  23.     CTFramesetterRef ctFramesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attributedString);  
  24.       
  25.     CGMutablePathRef path = CGPathCreateMutable();  
  26.     CGRect bounds = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);  
  27.     CGPathAddRect(path, NULL, bounds);  
  28.       
  29.     CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFramesetter,CFRangeMake(0, 0), path, NULL);  
  30.     CTFrameDraw(ctFrame, context);  
  31.       
  32.     CFArrayRef lines = CTFrameGetLines(ctFrame);  
  33.     CGPoint lineOrigins[CFArrayGetCount(lines)];  
  34.     CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), lineOrigins);  
  35.     NSLog(@"line count = %ld",CFArrayGetCount(lines));  
  36.     for (int i = 0; i < CFArrayGetCount(lines); i++) {  
  37.         CTLineRef line = CFArrayGetValueAtIndex(lines, i);  
  38.         CGFloat lineAscent;  
  39.         CGFloat lineDescent;  
  40.         CGFloat lineLeading;  
  41.         CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, &lineLeading);  
  42.         NSLog(@"ascent = %f,descent = %f,leading = %f",lineAscent,lineDescent,lineLeading);  
  43.           
  44.         CFArrayRef runs = CTLineGetGlyphRuns(line);  
  45.         NSLog(@"run count = %ld",CFArrayGetCount(runs));  
  46.         for (int j = 0; j < CFArrayGetCount(runs); j++) {  
  47.             CGFloat runAscent;  
  48.             CGFloat runDescent;  
  49.             CGPoint lineOrigin = lineOrigins[i];  
  50.             CTRunRef run = CFArrayGetValueAtIndex(runs, j);  
  51.             NSDictionary* attributes = (NSDictionary*)CTRunGetAttributes(run);  
  52.             CGRect runRect;  
  53.             runRect.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0,0), &runAscent, &runDescent, NULL);  
  54.             NSLog(@"width = %f",runRect.size.width);  
  55.               
  56.             runRect=CGRectMake(lineOrigin.x + CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL), lineOrigin.y - runDescent, runRect.size.width, runAscent + runDescent);  
  57.               
  58.             NSString *imageName = [attributes objectForKey:@"imageName"];  
  59.             //图片渲染逻辑  
  60.             if (imageName) {  
  61.                 UIImage *image = [UIImage imageNamed:imageName];  
  62.                 if (image) {  
  63.                     CGRect imageDrawRect;  
  64.                     imageDrawRect.size = image.size;  
  65.                     imageDrawRect.origin.x = runRect.origin.x + lineOrigin.x;  
  66.                     imageDrawRect.origin.y = lineOrigin.y;  
  67.                     CGContextDrawImage(context, imageDrawRect, image.CGImage);  
  68.                 }  
  69.             }  
  70.         }  
  71.     }  
  72.       
  73.     CFRelease(ctFrame);  
  74.     CFRelease(path);  
  75.     CFRelease(ctFramesetter);  
  76. }  

效果:



从上面看大家可能没有发现什么问题,当把图片放在字的最左边会是什么样子的?


因此为了避免这种情况发生,我在代码中添加了换行模式。添加换行后的效果:


更多 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值