[转] iOS文字排版(CoreText)那些事儿
文章转载自 http://www.cocoachina.com/applenews/devnews/2014/0521/8504.html
iOS文字排版(CoreText)那些事儿
转自阿毛的蛋疼地
第一次比较深入接触iOS文字排版相关内容是在12年底,实现某IM项目聊天内容的图文混排,照着nimbus的AttributedLabel和Raywenderlish上的这篇文章《Core Text Tutorial for iOS: Making a Magazine App》改出了一个比较适用于聊天内容展现的图文混排(文字和表情)控件。





- - (void)appendAttachment: (M80AttributedLabelAttachment *)attachment
- {
- attachment.fontAscent = _fontAscent;
- attachment.fontDescent = _fontDescent;
- unichar objectReplacementChar = 0xFFFC;
- NSString *objectReplacementString = [NSString stringWithCharacters:&objectReplacementChar length:1];
- NSMutableAttributedString *attachText = [[NSMutableAttributedString alloc]initWithString:objectReplacementString];
- CTRunDelegateCallbacks callbacks;
- callbacks.version = kCTRunDelegateVersion1;
- callbacks.getAscent = ascentCallback;
- callbacks.getDescent = descentCallback;
- callbacks.getWidth = widthCallback;
- callbacks.dealloc = deallocCallback;
- CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, (void *)attachment);
- NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)delegate,kCTRunDelegateAttributeName, nil];
- [attachText setAttributes:attr range:NSMakeRange(0, 1)];
- CFRelease(delegate);
- [_attachments addObject:attachment];
- [self appendAttributedText:attachText];
- }
- CGFloat ascentCallback(void *ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- CGFloat ascent = 0;
- CGFloat height = [image boxSize].height;
- switch (image.alignment)
- {
- case M80ImageAlignmentTop:
- ascent = image.fontAscent;
- break;
- case M80ImageAlignmentCenter:
- {
- CGFloat fontAscent = image.fontAscent;
- CGFloat fontDescent = image.fontDescent;
- CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
- ascent = height / 2 + baseLine;
- }
- break;
- case M80ImageAlignmentBottom:
- ascent = height - image.fontDescent;
- break;
- default:
- break;
- }
- return ascent;
- }
- CGFloat descentCallback(void *ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- CGFloat descent = 0;
- CGFloat height = [image boxSize].height;
- switch (image.alignment)
- {
- case M80ImageAlignmentTop:
- {
- descent = height - image.fontAscent;
- break;
- }
- case M80ImageAlignmentCenter:
- {
- CGFloat fontAscent = image.fontAscent;
- CGFloat fontDescent = image.fontDescent;
- CGFloat baseLine = (fontAscent + fontDescent) / 2 - fontDescent;
- descent = height / 2 - baseLine;
- }
- break;
- case M80ImageAlignmentBottom:
- {
- descent = image.fontDescent;
- break;
- }
- default:
- break;
- }
- return descent;
- }
- CGFloat widthCallback(void* ref)
- {
- M80AttributedLabelAttachment *image = (__bridge M80AttributedLabelAttachment *)ref;
- return [image boxSize].width;
- }
- - (void)drawAttachments
- {
- if ([_attachments count] == 0)
- {
- return;
- }
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- if (ctx == nil)
- {
- return;
- }
- CFArrayRef lines = CTFrameGetLines(_textFrame);
- CFIndex lineCount = CFArrayGetCount(lines);
- CGPoint lineOrigins[lineCount];
- CTFrameGetLineOrigins(_textFrame, CFRangeMake(0, 0), lineOrigins);
- NSInteger numberOfLines = [self numberOfDisplayedLines];
- for (CFIndex i = 0; i < numberOfLines; i++)
- {
- CTLineRef line = CFArrayGetValueAtIndex(lines, i);
- CFArrayRef runs = CTLineGetGlyphRuns(line);
- CFIndex runCount = CFArrayGetCount(runs);
- CGPoint lineOrigin = lineOrigins[i];
- CGFloat lineAscent;
- CGFloat lineDescent;
- CTLineGetTypographicBounds(line, &lineAscent, &lineDescent, NULL);
- CGFloat lineHeight = lineAscent + lineDescent;
- CGFloat lineBottomY = lineOrigin.y - lineDescent;
- // Iterate through each of the "runs" (i.e. a chunk of text) and find the runs that
- // intersect with the range.
- for (CFIndex k = 0; k < runCount; k++)
- {
- CTRunRef run = CFArrayGetValueAtIndex(runs, k);
- NSDictionary *runAttributes = (NSDictionary *)CTRunGetAttributes(run);
- CTRunDelegateRef delegate = (__bridge CTRunDelegateRef)[runAttributes valueForKey:(id)kCTRunDelegateAttributeName];
- if (nil == delegate)
- {
- continue;
- }
- M80AttributedLabelAttachment* attributedImage = (M80AttributedLabelAttachment *)CTRunDelegateGetRefCon(delegate);
- CGFloat ascent = 0.0f;
- CGFloat descent = 0.0f;
- CGFloat width = (CGFloat)CTRunGetTypographicBounds(run,
- CFRangeMake(0, 0),
- &ascent,
- &descent,
- NULL);
- CGFloat imageBoxHeight = [attributedImage boxSize].height;
- CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, nil);
- CGFloat imageBoxOriginY = 0.0f;
- switch (attributedImage.alignment)
- {
- case M80ImageAlignmentTop:
- imageBoxOriginY = lineBottomY + (lineHeight - imageBoxHeight);
- break;
- case M80ImageAlignmentCenter:
- imageBoxOriginY = lineBottomY + (lineHeight - imageBoxHeight) / 2.0;
- break;
- case M80ImageAlignmentBottom:
- imageBoxOriginY = lineBottomY;
- break;
- }
- CGRect rect = CGRectMake(lineOrigin.x + xOffset, imageBoxOriginY, width, imageBoxHeight);
- UIEdgeInsets flippedMargins = attributedImage.margin;
- CGFloat top = flippedMargins.top;
- flippedMargins.top = flippedMargins.bottom;
- flippedMargins.bottom = top;
- CGRect attatchmentRect = UIEdgeInsetsInsetRect(rect, flippedMargins);
- id content = attributedImage.content;
- if ([content isKindOfClass:[UIImage class]])
- {
- CGContextDrawImage(ctx, attatchmentRect, ((UIImage *)content).CGImage);
- }
- else if ([content isKindOfClass:[UIView class]])
- {
- UIView *view = (UIView *)content;
- if (view.superview == nil)
- {
- [self addSubview:view];
- }
- CGRect viewFrame = CGRectMake(attatchmentRect.origin.x,
- self.bounds.size.height - attatchmentRect.origin.y - attatchmentRect.size.height,
- attatchmentRect.size.width,
- attatchmentRect.size.height);
- [view setFrame:viewFrame];
- }
- else
- {
- NSLog(@"Attachment Content Not Supported %@",content);
- }
- }
- }
- }
[转] iOS文字排版(CoreText)那些事儿的更多相关文章
- iOS:基于CoreText的排版引擎
一.CoreText的简介 CoreText是用于处理文字和字体的底层技术.它直接和Core Graphics(又被称为Quartz)打交道.Quartz是一个2D图形渲染引擎,能够处理OSX和iOS ...
- iOS开发-UITextView文字排版
UITextView文本排版 1.配置NSMutableParagraphStyle NSMutableParagraphStyle *MParaStyle = [[NSMutableParagrap ...
- (转)iOS7界面设计规范(10) - UI基础 - 文字排版与配色
明天就是周四了.貌似前几天还在恨周一呢.话说今天几乎开了一整天的会,正经事情没做多少:这种感觉比一整天从早到晚12个小时的忙碌于一件事情还要让人感到疲惫的对叭?那今天的iOS7设计规范更新又是一篇很简 ...
- amazeui学习笔记--css(基本样式3)--文字排版Typography
amazeui学习笔记--css(基本样式3)--文字排版Typography 一.总结 1.字体:amaze默认非 衬线字体(sans-serif) 2.引用块blockquote和定义列表:引用块 ...
- OpenJudge计算概论-文字排版
/*====================================================================== 文字排版 总时间限制: 1000ms 内存限制: 65 ...
- div介绍 盒子模型边框属性 CSS初始化 文字排版 边框调整 溢出
今天学习的div,了解了div是干什么用的掌握了什么是盒子模型,以及div的外边距内边距以及边框,运用div和CSS给文字排版,利用边框的来做图像,div溢出的处理 CSS初始化: 精确排版的时候用这 ...
- 【html】文字排版
Web开发过程中文字排版,默认的情况下,行末的长单词会撑开容器. 我们想要的是(像word一样.能够自动换行.既不撑大容器.也不强制拆开行末单词.并且不会隐藏行末单词的多余字母) ①不能撑开容器 ②完 ...
- bootstrap世界探索1——山川河流(文字排版)
世界到底是什么?其实世界很简单,正所谓一花一世界,一树一菩提,世界就在我们身边.造物神是伟大的,在我看来无论是HTML,css,js都可以看作是一个世界,但是他们是构成宏观世界不可或缺的,正如IU框架 ...
- iOS App开发的那些事儿2:如何搭建合适的框架
<iOS App开发的那些事儿>系列文章从更宏观的角度出发,不仅仅局限于具体某个功能.界面的实现,而是结合网易云信iOS端研发负责人多年的经验,从如何优化现有代码的角度出发,深度分析如何创 ...
随机推荐
- dynamic解析Http xml格式响应数据
继续上一篇 构建RESTful风格的WCF服务 ,咱已经把服务端的数据和服务准备好了,客户端调用 wcf rest接口后如何解析xml?下面使用dynamic关键字解析来至于WCF REST XML响 ...
- RabbitMQ 上手记录-part 1-基础概念
RabbitMQ是什么,不用多介绍了,毕竟名声在那,江湖地位摆着,搜索引擎收录着.为什么突然去学习这个框架了,毕竟工作中没有用得上(说来也惭愧,工作中开发的项目没有使用这个框架).但是作为互联网分布式 ...
- npm install 后缀
npm 全局安装与本地安装 npm install express # 本地安装 npm install express -g # 全局安装 本地安装 将安装包放在 ./node_modules 下( ...
- BATJ面试必会之并发篇
一.线程状态转换 新建(New) 可运行(Runnable) 阻塞(Blocking) 无限期等待(Waiting) 限期等待(Timed Waiting) 死亡(Terminated) 二.使用线程 ...
- visual studio 不能进入调试状态
解决Windows操作系统在处理回环地址 1. 第一种解决方案是禁用环回检查. 步骤如下 a) 依次单击“开始”和“运行”,键入 regedit,然后单击“确定” b) 在注册表编辑器中,找到并单击下 ...
- mongodb int型id 自增
mongo的c#客户端提供了接口IIdGenerator,有guid和objectid等几种实现,但没有int型id的实现 接口主要2个方法,一个IsEmpty返回bool,判断当前id值是否是空(估 ...
- CLR 中 线程的 ThreadState 解释
ThreadState Aborted 线程已停止 AbortedRequested 线程的 Thread.Abort() 方法已被调用,但线程还未停止. Background 线程在后台执行,与 ...
- Win8操作系统下IIS如何配置asp.net的运行环境(win7同样)
一.把鼠标放在电脑屏幕的左下角然后右击,弹出如下图菜单,选择“程序和功能”(快捷键win+X).(win7点击电脑左下角的“开始”,然后点击“控制面板”打开程序与功能界面): 二.进入程序与功能界面后 ...
- 用struct模块解决tcp的粘包问题
服务器端程序 import struct import socket sk = socket.socket() sk.bind(('127.0.0.1',9000)) sk.listen() conn ...
- 搭建本地svn
1. 下载并安装TortoiseSVN,下载地址为:http://tortoisesvn.net/downloads.html. 2. 在本地创建一个文件夹,作为SVN服务的文件夹. ...