Quartz2D 之 绘制文本

1. 基础概念

1.1. 字体(Font)

  同一大小、同一样式的字形的集合。

1.2. 字符(Character)

  字符表示信息本身,一般指某种编码,如Unicode编码。

1.3. 字形(Glyphs)

  字符+字体就会有个对应的图片,这个图片就是字形。

1.3. 字形描述集(Glyphs Metris)

  字形的各个参数:

  • 边框(Bounding Box):一个假想的边框,尽可能地容纳整个字形。
  • 基线(Baseline):一条假想的参照线,以此为基础进行字形的渲染。一般来说是一条横线。
  • 基础原点(Origin):基线上最左侧的点。
  • 行间距(Leading):行与行之间的间距。
  • 字间距(Kerning):字与字之间的距离,为了排版的美观,并不是所有的字形之间的距离都是一致的,但是这个基本步影响到我们的文字排版。
  • 上行高度(Ascent)和下行高度(Decent):一个字形最高点和最低点到基线的距离,前者为正数,而后者为负数。当同一行内有不同字体的文字时,就取最大值作为相应的值。

  lineHeight = Ascent + |Decent| + Leading

 

2. Core Text

  原来 CGContextDrawText 被废弃掉了,现在使用 Core Text 绘制文本和图片。

  参考:http://www.cocoachina.com/industry/20140521/8504.html

  参考:http://blog.csdn.net/fengsh998/article/details/8691823

2.1. 基础概念

  绘图接口有:CTFrameDraw 和 CTLineDraw。 

2.1.1. NSAttributedString

  从字面上就可以理解:包含了属性的NSString。具有的属性:粗/斜体、下划线、颜色、背景灯。每个属性都可以设置到一个字符区域范围上。

2.1.2. CTFrameSetter

  由 CTFramesetter 根据参数 CGPath 生成 CTFrame。

2.1.3. CTFrame

  CTFrame 由一行一行的 CTLine 组成,每个 CTLine 包含很多 CTRun 。CTRun是字形绘制的最小单位。

2.1.4. 简单示例

    NSString *str = @"This is a test of characterAttribute. 中文字符";  
    NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];  
      
    [mabstring beginEditing];  

    //对同一段字体进行多属性设置      
    //红色  
    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];  
    //斜体  
    CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);  
    [attributes setObject:(id)font forKey:(id)kCTFontAttributeName];  
    //下划线  
    [attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];  
      
    [mabstring addAttributes:attributes range:NSMakeRange(0, 4)];       
        
    NSRange kk = NSMakeRange(0, 4);  
      
    NSDictionary * dc = [mabstring attributesAtIndex:0 effectiveRange:&kk];  
[mabstring endEditing];
CTFramesetterRef framesetter
= CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring); CGMutablePathRef Path = CGPathCreateMutable(); CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-10)); CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL); //获取当前(View)上下文以便于之后的绘画,这个是一个离屏。 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context , CGAffineTransformIdentity); //压栈,压入图形状态栈中.每个图形上下文维护一个图形状态栈,并不是所有的当前绘画环境的图形状态的元素都被保存。图形状态中不考虑当前路径,所以不保存 //保存现在的上下文图形状态。不管后续对context上绘制什么都不会影响真正得屏幕。 CGContextSaveGState(context); //x,y轴方向移动 CGContextTranslateCTM(context , 0 ,self.bounds.size.height); //缩放x,y轴方向缩放,-1.0为反向1.0倍,坐标系转换,沿x轴翻转180度 CGContextScaleCTM(context, 1.0 ,-1.0); CTFrameDraw(frame,context); CGPathRelease(Path); CFRelease(framesetter);

 

2.2.  图文混排的实现

  CoreText 不能绘制图片,而是给图片预留位置,最终由 CoreGraphics完成绘制。

  步骤;

  1. 设置某区域的 AttributedString 为 kCTRunDelegateAttributedString
  2. 根据上步设置生成CTRun
  3. 在CTRun的回调获取信息
  4. 通过CoreGraphics绘图。

2.2.1. s

posted on 2015-12-13 15:48  大木哥  阅读(352)  评论(0编辑  收藏  举报

导航