【IOS学习】CoreText学习笔记(一)相关的类

借图和代码一用,来自:http://geeklu.com/2013/03/core-text/
 这个博客是写了一个相对完整的代码,但是由于它封装了一些功能,导致层级关系复杂了,虽然代码简洁、可复用性好,但是不是很利于我们学习Core Text 的相关知识。
这里用原始的方法一点点地查看Core Text的相关知识

- (void)drawRect:(CGRect)rect


{    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
 
    CGMutablePathRef path = CGPathCreateMutable();
//这里你需要创建一个用于绘制文本的路径区域。Mac 上的 Core Text 支持矩形图形等不同形状,但在 iOS 上只支持矩形。在这个示例中,你将通过 self.bounds 使用整个视图矩形区域创建 CGPath 引用。
    CGPathAddRect(path, NULL, self.bounds );     NSAttributedString* attString = [[[NSAttributedString alloc]
        initWithString:@"Hello core text world!"] autorelease];
//在 Core Text 中使用 NSAttributedString 而不是NSString,NSAttributedString 是一个非常强大的 NSString 派生类,它允许你对文本应用格式化属性。 现在我们还没有用到格式化,这里仅仅使用纯文本。
 
    CTFramesetterRef framesetter =
        CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);
//CTFramesetter 是使用 Core Text 绘制时最重要的类。它管理您的字体引用和文本绘制帧。 目前您需要了解 CTFramesetterCreateWithAttributedString 通过应用属性化文本创建 CTFramesetter 。 本节中,在 framesetter 之后通过一个所选的文本范围(这里我们选择整个文本)与需要绘制到的矩形路径创建一个帧。
    CTFrameRef frame =
        CTFramesetterCreateFrame(framesetter,
            CFRangeMake(0, [attString length]), path, NULL);
    CTFrameDraw(frame, context);
//CTFrameDraw 将 frame 描述到设备上下文。
 
    CFRelease(frame);
// Core Frame 下的对象需要自己释放
    CFRelease(path);
    CFRelease(framesetter);
}


这个例子使用的是继承UIView获得Context,然后再context中写了一段文字。关键的四步用注释写在代码后面

涉及到的类有以下几个

  • CGMutablePathRef
  • NSAttributedString
  • CTFramesetterRef
  • CTFrameRef

它们的关系如下图:

【IOS学习】CoreText学习笔记(一)相关的类_第1张图片


CGMutablePathRef
CoreGraphics 下的CGpath
 
一个类型用于展示多个图形path,这个类的实现细节被苹果隐藏了

定义:

typedef struct CGPath *CGMutablePathRef;  //mutable ['mju:təbl] adj. 易变的,不定的。path 可修改
typedef const struct CGPath *CGPathRef;

一个 graphics path 是一个数学模型,用于描述一些类的形状或者线条。CGpath CGMutablePath 都定义了画path的方法,要画一个Quartz Path 到一个Context:需要通过方法 CGContextAddPath 添加path 到 graphics context,然后调用context的drawing(画图)方法。

每一个笔画在graphics path 是由一组的线条和贝里叶曲线,叫做subpath。一个subpath有一个排好序的路径元素(path elements),它们代表了subpath 结构的一个画图步骤。
从一个矩形的一个角到另外一个角的一条线段是一个路径元素,每一个路径元素包含了一个起点(starting point),和一个终点(current point)
添加一个subpath 到一个可变的path, 应用调用CGPathMoveToPoint 设置起点,初始化starting point 和 current point。 通过CGPathAdd* 方法添加线条和曲线到subpath。当线段或者曲线被添加到subpath,subpath的current point 更新为线段或曲线的结束点。 subpath 的线条和曲线一直是连接着的,但它们不需要形成一个闭环的path。 关闭一个subpath 通过 CGPathCloseSubpath。关闭subpath 添加一条线条在start pint,和这些线条如何渲染。


创建和管理Paths 的方法

   * CGPathCreateMutable
   * CGPathCreateWithEllipseInRect
   * CGPathCreateWithRect
   * CGPathCreateWithRoundedRect
   * CGPathCreateCopy
   * CGPathCreateCopyByTransformingPath
   * CGPathCreateCopyByDashingPath
   * CGPathCreateCopyByStrokingPath
   * CGPathCreateMutableCopy
   * CGPathCreateMutableCopyByTransformingPath
   * CGPathRelease
   * CGPathRetai

更改Quartz Paths
   * CGPathAddArc
   * CGPathAddRelativeArc
   * CGPathAddArcToPoint
   * CGPathAddCurveToPoint
   * CGPathAddLines
   * CGPathAddLineToPoint
   * CGPathAddPath
   * CGPathAddQuadCurveToPoint
   * CGPathAddRect
   * CGPathAddRects
   * CGPathAddRoundedRect
   * CGPathApply
   * CGPathMoveToPoint
   * CGPathCloseSubpath
   * CGPathAddEllipseInRect
   * 得到Quartz Paths信息
   * CGPathEqualToPath
   * CGPathGetBoundingBox
   * CGPathGetPathBoundingBox
   * CGPathGetCurrentPoint
   * CGPathGetTypeID
   * CGPathIsEmpty
   * CGPathIsRect
   * CGPathContainsPoint

NSAttributedString

一个NSAttributedString 对象管理字符串、和特定文字(使用NSRange标注)的属性列表(如字体font,字距kerning)。一个相关的字段和它们的属性叫做一个attributed string。两个公开类,NSAttributedString 和 NSMutableAttributedStirng,分别定义只读(read-only)的attributed strings 和可修改的attributedString。

一个attributed string 通过键值对的KEY定义属性,使用Nsdictionary 对象存储给定的属性名字的值。你能分配一个属性键值对给一个用range标记的字段——取决于你的应用程序自定的attributes(可查看Attributed String Programming Guide)。同样你可以在 Core Text 框架使用attributed string。在IOS,基本的attribute keys 在"NSAttributedSting UIKit Addicitons Reference"中有描述。

在IOS6和之后,你能够使用attributed strings 把格式化的文字展示在text views、text fields以及其他视图上。Appkit 和 UIKit 都定义了基本的attributed string接口拓展,允许你在当前graphic context 使用。

NSAttriutedString 默认的字体是Helvetica 12,可能与系统默认的不同,这样你也许希望给你的应用程序创建一个新的非默认的属性。你也能够使用NSparagraphStyle 类和它的子类NSMutableParaphStyle 来压缩被用在NSAttributedString类中的段落和标尺

注意NSAttributedString 对象比较使用isEqual:方法。这个比较包含了字符相等比较和所有属性attributes比较。
查看“NSAttributedString UIKit Additions Reference”了解关于IOS下画attributed strings的方法和画attributed string 的常量。
NSAttributedString 类是通过“toll-free bridged”与Core Foundation的CFAttributedStringRef 匹配,更多相关查看“Toll-Free Bridging”。

符合方法

NSCoding
encodeWithCoder:
initWithCoder:
NSCopying
copyWithZone:
NSMutableCopying
mutableCopyWithZone:


创建NSAtrributedString对象
– initWithString:
– initWithAttributedString:
– initWithString:attributes:

检索字符信息
– string
– length
检索Attribute信息
– attributesAtIndex:effectiveRange:
– attributesAtIndex:longestEffectiveRange:inRange:
– attribute:atIndex:effectiveRange‘;、:
– attribute:atIndex:longestEffectiveRange:inRange:
比较Atributed stirng
– isEqualToAttributedString:
摘取Substring
– attributedSubstringFromRange:
提取一个string 中的 Attributes
– enumerateAttribute:inRange:options:usingBlock:
– enumerateAttributesInRange:options:usingBlock:


CTFramesetter Reference

CTFramesetter 类型用于生成text frames,CTFramesetter是CTFrame对象的对象工厂 。CTFramesetter 获取attributed类型对象和一个形状描述对象,创建line 对象填充形状。输出是一个包含了一个line数组的frame对象,frame 可以直接把自己画在graphic context。
创建CTFramesetter
  CTFramesetterCreateWithAttributedString
创建Frames
  CTFramesetterCreateFrame
  CTFramesetterGetTypesetter
frame大小
  CTFramesetterSuggestFrameSizeWithConstraints
得到类型id
  CTFramesetterGetTypeID

CTFrame Reference


一个CTFrame类型代表了包含多行text的frame。这个frame 对对象是由framesetter 对象生成的
能够画整个text frame 到当前的graphic context,这个frame对象包含了多行数组,这些数组能够检索单个的渲染和字形信息
 得到frame信息
   * CTFrameGetStringRange
   * CTFrameGetVisibleStringRange
   * CTFrameGetPath
   * CTFrameGetFrameAttributes
 得到行的数组
   * CTFrameGetLines
   * CTFrameGetLineOrigins
 画
   * CTFrameDraw
 得到type id
   * CTFrameGetTypeID


参考资料: http://geeklu.com/2013/03/core-text/

Demo 下载地址:https://github.com/caigee/iosdev_sample

下的DemoCoreText

厚吾http://blog.csdn.net/mangosnow

本文遵循“署名-非商业用途-保持一致”创作公用协议


你可能感兴趣的:(IOS学习,Core,Text,类,学习笔记)