[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. CH3301 同余方程

    题意 3301 同余方程 0x30「数学知识」例题 描述 求关于 x的同余方程  ax ≡ 1(mod b) 的最小正整数解. 输入格式 输入只有一行,包含两个正整数a,b,用一个空格隔开. 输出格式 ...

  2. Pacemaker+Corosync搭建PostgreSQL集群

    https://my.oschina.net/aven92/blog/518928 · PostgreSQL中国社区: http://postgres.cn/index.php/home · Post ...

  3. RabbitMQ内存爆出问题解决思路

    http://www.bubuko.com/infodetail-2121050.html RabbitMQ升级到3.6.1版本后,随着业务和系统功能的增加,出现RabbitMQ内存陡增直至服务宕掉的 ...

  4. 【python】使用HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies

    一.从HTML文档中提取链接 模块HTMLParser,该模块使我们能够根据HTML文档中的标签来简洁.高效地解析HTML文档. 处理HTML文档的时候,我们常常需要从其中提取出所有的链接.使用HTM ...

  5. emacs里面模拟vim按键操作的插件evil

    emacsConfig/evil-setting.el (setq evil-mode t) (setq evil-shift-width ) ;; some modes aren't meant f ...

  6. 在vs2010中编译log4cxx-0.10.0详细方法(从下载、编译、解决错误详细介绍)

    在vs2010中编译log4cxx-0.10.0详细方法(从下载.编译.解决错误详细介绍) http://blog.sina.com.cn/s/blog_a459dcf501013tbn.html

  7. VS2015 安装包

    http://download.microsoft.com/download/D/C/9/DC99C86F-5E93-4F77-AF7A-05AAC9BD8B72/vs2015.1.ent_chs.i ...

  8. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  9. pythonNet day02

    网络收发缓冲区 1.协调读写速度.减少和磁盘交互 2.recv和send实际上是从缓冲区获取内容,和向缓冲区发送内容 recv()特性 1.如果连接断开,recv会立即结束阻塞返回空字符串 2.当接收 ...

  10. android studio 3.0.1使用笔记(一)20171231

    首先安装JDK1.8.1并设置环境变量JAVA_HOME(C:/JAVA/JDK)及PATH(;%JAVA_HOME%\bin ) 然后安装AS 然后首次AS运行并退出,将GRADLE-4-1.ZIP ...