国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS 圖文混排(CoreText.framework)詳解及實例

2020-07-26 03:04:49
字體:
來源:轉載
供稿:網友

IOS 圖文混排(CoreText.framework)

       本文主要介紹了IOS圖文混排的資料,這里整理了在網上查找的內容,幫助理解,掌握這部分知識,以下就是整理的內容:   

利用CORETEXT進行圖文混排。

實現代碼:

void RunDelegateDeallocCallback( void* refCon ){    }  CGFloat RunDelegateGetAscentCallback( void *refCon ){   NSString *imageName = (NSString *)refCon;   return 80;//[UIImage imageNamed:imageName].size.height; }  CGFloat RunDelegateGetDescentCallback(void *refCon){   return 0; }  CGFloat RunDelegateGetWidthCallback(void *refCon){   NSString *imageName = (NSString *)refCon;   return 100;//[UIImage imageNamed:imageName].size.width; } 

先設置一個CTRun的委托,主要是用于指定對象的上行高,寬,或上下文釋放時使用。

-(void)drawCharAndPicture {   CGContextRef context = UIGraphicsGetCurrentContext();      CGContextSetTextMatrix(context, CGAffineTransformIdentity);//設置字形變換矩陣為CGAffineTransformIdentity,也就是說每一個字形都不做圖形變換      CGAffineTransform flipVertical = CGAffineTransformMake(1,0,0,-1,0,self.bounds.size.height);   CGContextConcatCTM(context, flipVertical);//將當前context的坐標系進行flip   NSLog(@"bh=%f",self.bounds.size.height);      NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"請在這里插入一張圖片位置"] autorelease];         //為圖片設置CTRunDelegate,delegate決定留給圖片的空間大小   NSString *imgName = @"img.png";   CTRunDelegateCallbacks imageCallbacks;   imageCallbacks.version = kCTRunDelegateVersion1;   imageCallbacks.dealloc = RunDelegateDeallocCallback;   imageCallbacks.getAscent = RunDelegateGetAscentCallback;   imageCallbacks.getDescent = RunDelegateGetDescentCallback;   imageCallbacks.getWidth = RunDelegateGetWidthCallback;   CTRunDelegateRef runDelegate = CTRunDelegateCreate(&imageCallbacks, imgName);   NSMutableAttributedString *imageAttributedString = [[NSMutableAttributedString alloc] initWithString:@" "];//空格用于給圖片留位置   [imageAttributedString addAttribute:(NSString *)kCTRunDelegateAttributeName value:(id)runDelegate range:NSMakeRange(0, 1)];   CFRelease(runDelegate);      [imageAttributedString addAttribute:@"imageName" value:imgName range:NSMakeRange(0, 1)];      [attributedString insertAttributedString:imageAttributedString atIndex:4]; 
 //換行模式   CTParagraphStyleSetting lineBreakMode;   CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping;   lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;   lineBreakMode.value = &lineBreak;   lineBreakMode.valueSize = sizeof(CTLineBreakMode);      CTParagraphStyleSetting settings[] = {     lineBreakMode   };      CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1);           // build attributes   NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)style forKey:(id)kCTParagraphStyleAttributeName ];      // set attributes to attributed string   [attributedString addAttributes:attributes range:NSMakeRange(0, [attributedString length])];          CTFramesetterRef ctFramesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attributedString);      CGMutablePathRef path = CGPathCreateMutable();   CGRect bounds = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);   CGPathAddRect(path, NULL, bounds);      CTFrameRef ctFrame = CTFramesetterCreateFrame(ctFramesetter,CFRangeMake(0, 0), path, NULL);   CTFrameDraw(ctFrame, context);      CFArrayRef lines = CTFrameGetLines(ctFrame);   CGPoint lineOrigins[CFArrayGetCount(lines)];   CTFrameGetLineOrigins(ctFrame, CFRangeMake(0, 0), lineOrigins);   NSLog(@"line count = %ld",CFArrayGetCount(lines));   for (int i = 0; i < CFArrayGetCount(lines); i++) {     CTLineRef line = CFArrayGetValueAtIndex(lines, i);     CGFloat lineAscent;     CGFloat lineDescent;     CGFloat lineLeading;     CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, &lineLeading);     NSLog(@"ascent = %f,descent = %f,leading = %f",lineAscent,lineDescent,lineLeading);          CFArrayRef runs = CTLineGetGlyphRuns(line);     NSLog(@"run count = %ld",CFArrayGetCount(runs));     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);       NSLog(@"width = %f",runRect.size.width);              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;           CGContextDrawImage(context, imageDrawRect, image.CGImage);         }       }     }   }      CFRelease(ctFrame);   CFRelease(path);   CFRelease(ctFramesetter); } 

效果:


從上面看大家可能沒有發現什么問題,當把圖片放在字的最左邊會是什么樣子的?


因此為了避免這種情況發生,我在代碼中添加了換行模式。添加換行后的效果:


感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵丘县| 泽州县| 桐庐县| 德阳市| 安龙县| 新田县| 芷江| 茂名市| 腾冲县| 当阳市| 丰县| 江安县| 西丰县| 石屏县| 藁城市| 鄂托克旗| 朝阳区| 芮城县| 安达市| 甘南县| 太仓市| 高平市| 舟山市| 阿勒泰市| 玉龙| 方正县| 承德县| 洛阳市| 图木舒克市| 汉沽区| 罗山县| 武威市| 南雄市| 恭城| 丰台区| 神池县| 新巴尔虎左旗| 邮箱| 遵义县| 开远市| 抚宁县|