[CoreText]

   Core Text is designed for development of higher-level text-handling frameworks. General app developers should use Text Kit in iOS (see Text Programming Guide for iOS). Core Text mediates between text layout and font support provided by higher level frameworks and the low-level capabilities Quartz provides to all text and font frameworks. Quartz is the only way to get glyphs drawn at a fundamental level.

  The Core Text layout engine often works with attributed strings (CFAttributedStringRef) and graphics paths (CGPathRef). The graphics path defines the shape of a frame of text. With an attributed string and a graphics path as input, a framesetter generates one or more frames of text (CTFrameRef). Each CTFrame object represents a paragraph.

  

  

  To generate frames, the framesetter calls a typesetter object (CTTypesetterRef). As it lays text out in a frame, the framesetter applies paragraph styles to it, including such attributes as alignment, tab stops, line spacing, indentation, and line-breaking mode. The typesetter converts the characters in the attributed string to glyphs and fits those glyphs into the lines that fill a text frame.

  Each CTFrame object contains the paragraph’s line (CTLine) objects. Each line object represents a line of text. A CTFrame object may contain just a single long CTLine object or it might contain a set of lines. Line objects are created by the typesetter during a framesetting operation and, like frames, can draw themselves directly into a graphics context.

  Each CTLine object contains an array of glyph run (CTRun) objects. A glyph run is a set of consecutive glyphs that share the same attributes and direction. The typesetter creates glyph runs as it produces lines from character strings, attributes, and font objects. This means that a line is constructed of one or more glyphs runs. Glyph runs can draw themselves into a graphic context, if desired, although most clients have no need to interact directly with glyph runs.

[Font Objects]

  

  You can think of font descriptors as queries into the font system. You can create a font descriptor with an incomplete specification, that is, with one or just a few values in the attribute dictionary, and the system will choose the most appropriate font from those available. For example, if you make a query using a descriptor for the name of family with the standard faces (normal, bold, italic, bold italic), not specifying any traits would match all faces in the family, but if you specify a traits dictionary with akCTFontTraitsAttribute of kCTFontTraitBold, the results are further narrowed from the whole family to its members satisfying the bold trait. The system can give you a complete list of font descriptors matching your query via CTFontDescriptorCreateMatchingFontDescriptors.

  Create a font collection of all the fonts available in the system by calling CTFontCollectionCreateFromAvailableFonts, and you can use the collection to obtain an array of all of the member font descriptors.

[Layout Opeartion]

  One of the most common operations in typesetting is laying out a multiline paragraph within an arbitrarily sized rectangular area.  To lay out the paragraph, you need a graphics context to draw into, a rectangular path to provide the area where the text is laid out, and an attributed string.

 // Initialize a graphics context in iOS.
CGContextRef context = UIGraphicsGetCurrentContext(); // Flip the context coordinates, in iOS only.
CGContextTranslateCTM(context, , self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0); // Initializing a graphic context in OS X is different:
// CGContextRef context =
// (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; // Set the text matrix.
CGContextSetTextMatrix(context, CGAffineTransformIdentity); // Create a path which bounds the area where you will be drawing text.
// The path need not be rectangular.
CGMutablePathRef path = CGPathCreateMutable(); // In this simple example, initialize a rectangular path.
CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);
CGPathAddRect(path, NULL, bounds ); // Initialize a string.
CFStringRef textString = CFSTR("Hello, World! I know nothing in the world that has as much power as a word. Sometimes I write one, and I look at it, until it begins to shine."); // Create a mutable attributed string with a max length of 0.
// The max length is a hint as to how much internal storage to reserve.
// 0 means no hint.
CFMutableAttributedStringRef attrString =
CFAttributedStringCreateMutable(kCFAllocatorDefault, ); // Copy the textString into the newly created attrString
CFAttributedStringReplaceString (attrString, CFRangeMake(, ),
textString); // Create a color that will be added as an attribute to the attrString.
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = { 1.0, 0.0, 0.0, 0.8 };
CGColorRef red = CGColorCreate(rgbColorSpace, components);
CGColorSpaceRelease(rgbColorSpace); // Set the color of the first 12 chars to red.
CFAttributedStringSetAttribute(attrString, CFRangeMake(, ),
kCTForegroundColorAttributeName, red); // Create the framesetter with the attributed string.
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString(attrString);
CFRelease(attrString); // Create a frame.
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(, ), path, NULL); // Draw the specified frame in the given context.
CTFrameDraw(frame, context); // Release the objects we used.
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);

  A nother common typesetting operation is drawing a single line of text to use as a label for a user-interface element. In Core Text this requires only two lines of code: one to create the line object with a CFAttributedString and another to draw the line into a graphic context.

 CFStringRef string; CTFontRef font; CGContextRef context;
// Initialize the string, font, and context CFStringRef keys[] = { kCTFontAttributeName };
CFTypeRef values[] = { font }; CFDictionaryRef attributes =
CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys,
(const void**)&values, sizeof(keys) / sizeof(keys[]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks); CFAttributedStringRef attrString =
CFAttributedStringCreate(kCFAllocatorDefault, string, attributes);
CFRelease(string);
CFRelease(attributes); CTLineRef line = CTLineCreateWithAttributedString(attrString); // Set text position and draw the line into the graphics context
CGContextSetTextPosition(context, 10.0, 10.0);
CTLineDraw(line, context);
CFRelease(line);

[总结]

  App应用开发如非必要, 尽量避免使用Core  Text, 优先使用 Text Kit。

CoreText的更多相关文章

  1. 图文混排--CoreText的简单运用

    常见的在一些微博微信中可以看见一段文字中有不同的字体,字体有不同的颜色,并且可能会有一些笑脸之类的表情,这些可以通过图文混排做到. 图文混排可以通过WebView和CoreText做到,其他还有别的方 ...

  2. iOS:基于CoreText的排版引擎

    一.CoreText的简介 CoreText是用于处理文字和字体的底层技术.它直接和Core Graphics(又被称为Quartz)打交道.Quartz是一个2D图形渲染引擎,能够处理OSX和iOS ...

  3. Coretext实现图文混排及Gif图片播放

    CoreText是iOS3.2推出的一套文字排版和渲染框架,可以实现图文混排,富文本显示等效果. CoreText中的几个重要的概念:  CTFont CTFontCollection CTFontD ...

  4. 【iOS】使用CoreText实现图文混排

    iOS没有现成的支持图文混排的控件,而要用多个基础控件组合拼成图文混排这样复杂的排版,是件很苦逼的事情.对此的解决方案有使用CoreText进行绘制,或者使用TextKit.本文主要讲解对于CoreT ...

  5. CoreText原理及基本使用方法

    关于富文本的排版也是现在的一个技术点,以下是近日关于CoreText的学习记录以及个人理解,希望能对正在学习CoreText的朋友起到帮助. 1.框架坐标系 首先让我们先来看看CoreText坐标系和 ...

  6. iOS阅读器实践系列(一)coretext纯文本排版基础

    前言:之前做了公司阅读类的App,最近有时间来写一下阅读部分的实现过程,供梳理逻辑,计划会写一个系列希望能涉及到尽量多的方面与细节,欢迎大家交流.吐槽.拍砖,共同进步. 阅读的排版用的是coretex ...

  7. 基于CoreText的基础排版引擎

    storyboard: 新建一个CTDisplayView:UIView 代码如下: #import "CTDisplayView.h" #import "CoreTex ...

  8. iOS开发 coreText

    coreText的demo下载地址:http://download.csdn.net/detail/shaoting19910730/9254143 NSTextView和Attribued Stri ...

  9. CoreText 使用教程

    [iOS开发] CoreText 使用教程:以创建一个简单的杂志应用为例抢沙发 分类:iPhone开发 标签:CoreText.iOS.iOS开发.iOS开发教程.杂志应用 BBS.CHINAAPP. ...

  10. IOS 使用CoreText实现表情文本URL等混合显示控件

    实现了一个富文本视图控件.主要针对表情图片,文本字符,URL,等这种类型的文本进行显示. 源码地址 https://github.com/TinyQ/TQRichTextView 实现的效果如下图. ...

随机推荐

  1. 使用树莓派制作一套“NAS+私有云盘+下载机”

    ‍ ‍‍原创作者:HackLiu‍‍ ‍ 0×00 前言 ‍‍如果你家里有多台设备需要联网需要娱乐,你一定会或多或少遇到设备碎片化带来的烦恼.当然,已经有很多厂商包括新晋的小米.360在内的互联网公司 ...

  2. table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)

    table中的tr的默认display:table-row,虽然可以修改为display:block但是就失去了tr特有的显示效果,如(td自动对齐): 并且在tr中对起设定padding是有用的,可 ...

  3. oracle之 RAC本地数据文件迁移至ASM

    系统环境:CentOS release 6.7 (Final)Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit 操作过 ...

  4. python的面向对象

    1.self 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是在调用这个方法的时候你不为这个参数赋值,Python会提供这个值.这个特别的变量指对象本身,按照惯例它的 ...

  5. 在laravel之外使用eloquent

    视频地址 https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel

  6. lucene相关度算法

    一.这篇博客已经讲的很清楚 http://blog.csdn.net/starzhou/article/details/51543209 补充:q:就是一个查询比如是hello world 则  t: ...

  7. Devexpres下窗体带阴影的边框效果

    public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent( ...

  8. Envoy 源码分析--程序启动过程

    目录 Envoy 源码分析--程序启动过程 初始化 main 入口 MainCommon 初始化 服务 InstanceImpl 初始化 启动 main 启动入口 服务启动流程 LDS 服务启动流程 ...

  9. ToastUtil

    将Toast的调用封装成一个接口,写在一个公共的类当中 public class Util { private static Toast toast; public static void showT ...

  10. Java 8 字符串日期排序

    public class ObjectDto implements Serializable { private static final long serialVersionUID = 858983 ...