CoreText使用介绍

一、概述
  
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值