打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS
51、CoreText 文字填充不规则图形

 CGContextRef context = UIGraphicsGetCurrentContext();


   // Flip the coordinate system
   CGContextSetTextMatrix(context, CGAffineTransformIdentity);
   CGContextTranslateCTM(context,0, self.bounds.size.height);
   CGContextScaleCTM(context,1.0, -1.0);

   // Create a path to render text in
   CGMutablePathRef path = CGPathCreateMutable();
   CGPathAddRect(path, NULL, self.bounds );
   
   // An attributed string containing the text to render
   NSAttributedString* attString = [[NSAttributedString alloc]
                                     initWithString:...];
   
   // Create a path to wrap around
   CGMutablePathRef clipPath = CGPathCreateMutable();
   CGPathAddEllipseInRect(clipPath, NULL, CGRectMake(200,200,300,300) );

   // A CFDictionary containing the clipping path
   CFStringRef keys[] = { kCTFramePathClippingPathAttributeName };
   CFTypeRef values[] = { clipPath };
   CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL,
            (constvoid **)&keys, (constvoid **)&values,
             sizeof(keys) /sizeof(keys[0]),
             &kCFTypeDictionaryKeyCallBacks,
             &kCFTypeDictionaryValueCallBacks);

   // An array of clipping paths -- you can use more than one if needed!
   NSArray *clippingPaths = [NSArray arrayWithObject:(NSDictionary*)clippingPathDict];
   
   // Create an options dictionary, to pass in to CTFramesetter
   NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:clippingPaths forKey:(NSString*)kCTFrameClippingPathsAttributeName];

   // Finally create the framesetter and render text
   CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);//3
   CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
                            CFRangeMake(0, [attString length]), path, optionsDict);
   
   CTFrameDraw(frame, context);
   
   // Clean up
   CFRelease(frame);
   CFRelease(path);

   CFRelease(framesetter);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Android10填坑适配指南,实际经验代码,拒绝翻译
【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
28、CoreText 总结
iOS 图片压缩
iOS开发- 图片转PDF的实现方法
Android API :SMS短信服务处理和获取联系人
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服