一、概述

  
1.CoreText是苹果创建的一个用于文字排版的框架,可以实现文字排版、图文混排等复杂的界面效果。从iOS3.2启用。
2.一个开源工具类-OHAttributedLabel,就是使用CoreText框架实现的,能够实现一个Label中有不同的文字大小、文字颜色、字体以及链接等。  
二、一般使用步骤
1.创建NSMutableAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:contentString];
                
2.设置文字颜色                
[attributeString addAttribute:(id)kCTForegroundColorAttributeName
                        value:(id)[UIColor darkGrayColor].CGColor 
                        range:NSMakeRange(0, tempArticle.desc.length)];
2.设置字体以及大小                
CTFontRef font = CTFontCreateWithName(CFSTR("Bodoni 72"), contentFontSize, NULL);
[attributeString addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, [attributeString length])];
CFRelease(font);
4.初始化段落首行缩进样式
CGFloat headIndent = contentFontSize * 2;
CTParagraphStyleSetting headIndentStyle;
headIndentStyle.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent;
headIndentStyle.valueSize = sizeof(headIndent);
headIndentStyle.value = &headIndent;
            
5.初始化文字对齐方式            
CTTextAlignment alignment = kCTJustifiedTextAlignment;
CTParagraphStyleSetting alignmentStyle;
alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment;
alignmentStyle.valueSize = sizeof(alignment);
alignmentStyle.value = &alignment;
            
6.初始化行间距
CGFloat lineSpace = 12.0f;
CTParagraphStyleSetting lineSpaceStyle;
lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacing;
lineSpaceStyle.valueSize = sizeof(lineSpace);
lineSpaceStyle.value = &lineSpace;
            
7.初始化段间距
CGFloat paragraphSpace = 18;
CTParagraphStyleSetting paragraphSpaceStyle;
paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
paragraphSpaceStyle.valueSize = sizeof(paragraphSpace);
paragraphSpaceStyle.value = &paragraphSpace;
            
8.将段落属性设置到NSMutableAttributedString
CTParagraphStyleSetting settings[4] = {headIndentStyle,alignmentStyle,lineSpaceStyle,paragraphSpaceStyle};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate((const CTParagraphStyleSetting*)&settings,4);
[attributeString addAttribute:(id)kCTParagraphStyleAttributeName 
                        value:(id)paragraphStyle range:NSMakeRange(0, [attributeString length])];
CFRelease(paragraphStyle);
9.创建CTFramesetterRef            
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributeString);
            
10.绘制之前,翻转绘图坐标系
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
            
11.按照区域进行绘制       
CFIndex startIndex = 0; 
NSInteger pathCount = 0;
while (YES) {
   //构建绘图区域
   CGMutablePathRef columnPath = CGPathCreateMutable();
   CGPathAddRect(columnPath, NULL,
      CGRectMake(20 + (pathCount%columnNum) * ((768-(columnNum+1)*20)/columnNum + 20), 50, (768-(columnNum+1)*20)/columnNum, 904));
   //构建内容窗体
   CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(startIndex,0), columnPath, NULL);
   //绘制内容窗体
   CTFrameDraw(frame, context);
   //计算当前显示结束位置的字符索引
   CFRange currRange = CTFrameGetVisibleStringRange(frame);
   startIndex = startIndex + currRange.length;
   //释放
   CGPathRelease(columnPath);
   CFRelease(frame);
   //计数增加     
   pathCount++;
   //结束
   if (startIndex == [attributeString length]) {
      break;
   }
}
12.按照行进行绘制
CFIndex start = 0;
while (YES) {
   //判断是否绘制完毕
   if (start == attributeString.length) {
      break;
   }
   //根据内容、开始索引位置和绘制区域的宽度,返回推荐的换行位置索引
   CFIndex count = CTTypesetterSuggestLineBreak(frameSetter, start, pageWidth);
   //创建一个新行
   CTLineRef line = CTTypesetterCreateLine(frameSetter, CFRangeMake(start, count));
   //获取新行的排版属性     
   CGFloat ascent;
   CGFloat descent;
   CGFloat leading;
   CTLineGetTypographicBounds(line, &ascent,  &descent, &leading);
   //计算新行的Y值                 
   imageY = imageY - lineSpace - ascent - descent - leading;
   //绘制行                 
   CGContextSetTextPosition(currContext, 0.0f, imageY);
   CTLineDraw(line, currContext);
   //释放行对象                 
   CFRelease(line);
   //更改当前绘制的位置索引                 
   start += count;
}

CoreText使用介绍相关推荐

  1. CoreText入坑一

    CoreText是Mac OS和iOS系统中处理文本的low-level API, 不管是使用OC还是swift, 实际我们使用CoreText都还是间接或直接使用C语言在写代码.CoreText是i ...

  2. 使用CoreText实现图文混排

    2019独角兽企业重金招聘Python工程师标准>>> OS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用Co ...

  3. iOS总体框架介绍和详尽说明

    总体介绍 iOS为应用程序开发提供了许多可使用的框架,并构成IOS操作系统的层次架构,分为四层,从上到下依次为:Cocoa Touch Layer(触摸UI层).MediaLayer(媒体层).Cor ...

  4. iOS 常用框架介绍

    iOS框架介绍      Cocoa Touch GameKit  实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息  iOS设备之间蓝牙数据传输   从iOS7开始过期   局域网游戏 ...

  5. ios 关于常用的一些第三方框架的介绍

    本文转载自:http://blog.csdn.net/xiaoyuertongxue/article/details/46982879 图像:  1.图片浏览控件MWPhotoBrowser 实现了一 ...

  6. iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)

    图像:  1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作.  ...

  7. (转)iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)

    图像: 1.图片浏览控件MWPhotoBrowser  实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. ...

  8. iOS开发-常用第三方开源框架介绍(2)

    2019独角兽企业重金招聘Python工程师标准>>> Frank 自动化测试工具?  superdb 一个Debuger工具 用命令行调试..  iOS-Hierarchy-Vie ...

  9. iOS中运用coreText 进行文字自适应

    先看看效果图 这里可以指定显示的宽度,高度随着文字的数量自动增加 看到这些是不是很开心,IM聊天记录基本都是这样的原理. 随着输入的字体自动增加,显示的View的高度自动动态的增加 : 这里介绍一下c ...

最新文章

  1. android html.fromhtml 字体加粗,Android Html设置TextView的颜色、加粗样式
  2. 深度学习之PyTorch物体检测
  3. java学习曲线建议
  4. 二次拟合r方_R方和线性回归拟合优度
  5. Qt创建工程及导入资源图片
  6. perl中DBD-oracle安装,Linux下安装perl的DBI和DBD
  7. eclipse--基本配置
  8. 深度解析艾瑞咨询《2017年度中国商业智能行业研究报告》
  9. 通过CSS样式隐藏百度版权标志
  10. The Child and Toy
  11. JavaScript词法分析
  12. STM32F4 + HAL库 + W25Q256的验证
  13. PL/0语言 语义分析及中间代码生成
  14. linux中国共享文件,linux上的文件共享服务详解
  15. 新浪短网址php源码,新浪短网址api接口腾讯短网址api接口分享
  16. Procmon 结果列代码及其含义Detail 列使用的文件属性代码
  17. 数据库表的软硬关联_Jimmy的关系型数据库设计心得 第一版
  18. batchupdate写法_mybatis执行批量插入insert和批量更新update
  19. linux下建立软链接
  20. [转]Android 开源项目分类汇总

热门文章

  1. Linux基础概念及常用命令
  2. WinForm中DataGridView的TextBoxColumm换行
  3. textarea标签内的文字无缘故居中解决原因
  4. 也来分析为什么支付宝要做社交
  5. 虚拟机中Linux安装Tools
  6. VS2005 添加 Microsoft.Office.Tools.Word.dll 等引用
  7. 4.1_ 1_ 初识文件管理
  8. 3.2_ 2_ 请求分页管理方式
  9. java swing 多个线程,Swing与多线程
  10. linux实验总结及心得_安全实验室 | 内网渗透—Linux权限维持技巧总结