ios NSAttributedString 具体解释

NSAttributedString能够让我们使一个字符串显示的多样化,可是眼下到iOS 5为止,好像对它支持的不是非常好,由于显示起来不太方便(至少没有在OS X上方便)。

首先导入CoreText.framework,并在须要使用的文件里导入:

#import<CoreText/CoreText.h>

创建一个NSMutableAttributedString:

  1. NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]
  2. autorelease];

很常规的创建方式,接下来我们给它配置属性:

  1. //把this的字体颜色变为红色
  2. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
  3. value:(id)[UIColor redColor].CGColor
  4. range:NSMakeRange(0, 4)];
  5. //把is变为黄色
  6. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName
  7. value:(id)[UIColor yellowColor].CGColor
  8. range:NSMakeRange(5, 2)];
  9. //改变this的字体,value必须是一个CTFontRef
  10. [attriString addAttribute:(NSString *)kCTFontAttributeName
  11. value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,
  12. 14,
  13. NULL)
  14. range:NSMakeRange(0, 4)];
  15. //给this加上下划线,value能够在指定的枚举中选择
  16. [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
  1. value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]
  2. range:NSMakeRange(0, 4)];
  3. return attriString;

这样就算是配置好了,可是我们能够发现NSAttributedString继承于NSObject,而且不支持不论什么draw的方法,那我们就仅仅能自己draw了。写一个UIView的子类(如果命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:

  1. -(void)drawRect:(CGRect)rect{
  2. [super drawRect:rect];
  3. NSAttributedString *attriString = getAttributedString();
  4. CGContextRef ctx = UIGraphicsGetCurrentContext();
  5. CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));
  6. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);
  7. CGMutablePathRef path = CGPathCreateMutable();
  8. CGPathAddRect(path, NULL, rect);
  9. CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
  10. CFRelease(path);
  11. CFRelease(framesetter);
  12. CTFrameDraw(frame, ctx);
  13. CFRelease(frame);
  14. }

在代码中我们调整了CTM(current transformation matrix),这是由于Quartz 2D的坐标系统不同,比方(10, 10)到(20, 20)的直线坐标:

坐标类似于数学中的坐标,能够先不调整CTM,看它是什么样子的,以下两种调整方法是全然一样的:

  1. CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));

==

  1. CGContextTranslateCTM(ctx, 0, rect.size.height);
  2. CGContextScaleCTM(ctx, 1, -1);

CTFramesetter是CTFrame的创建工厂,NSAttributedString须要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。

假设想要计算NSAttributedString所要的size,就须要用到这个API:

CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,由于在CoreText里,行间距也是你来控制的。

设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为非常多子

属性,当中就包含

  • kCTLineBreakByCharWrapping
  • kCTParagraphStyleSpecifierLineSpacingAdjustment
设置例如以下:
  1. //段落
  2. //line break
  3. CTParagraphStyleSetting lineBreakMode;
  4. CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式
  5. lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;
  6. lineBreakMode.value = &lineBreak;
  7. lineBreakMode.valueSize = sizeof(CTLineBreakMode);
  8. //行间距
  9. CTParagraphStyleSetting LineSpacing;
  10. CGFloat spacing = 4.0;  //指定间距
  11. LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
  12. LineSpacing.value = &spacing;
  13. LineSpacing.valueSize = sizeof(CGFloat);
  14. CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing};
  15. CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2);   //第二个參数为settings的长度
  16. [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName
  17. value:(id)paragraphStyle
  18. range:NSMakeRange(0, attributedString.length)];

-----------------------------------------猥琐的分界线-----------------------------------------

这并非唯一的方法,还有还有一种替代方案:

  1. CATextLayer *textLayer = [CATextLayer layer];
  2. textLayer.string = getAttributedString();
  3. textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200);
  4. [self.view.layer addSublayer:textLayer];

CATextLayer能够直接支持NSAttributedString!

-----------------------------------------猥琐的分界线-----------------------------------------

效果图:

源代码地址

-----------------------------------------猥琐的分界线-----------------------------------------
眼下发现有一个问题,好像中文全都会被加粗,设置不加粗的字体也没用,应该是CoreText的bug,已经上报给了apple了。

-----------------------------------------对于cythb兄提出的问题-----------------------------------------
首先表示感谢关注

原文中确有描写叙述不适当的地方,比方:The upper-left corner of the context is (0, 0) 。实际上Quartz2D的坐标系统确实在左下角,仅仅是有一些技术在设置它们的graphics
context时使用了不同于Quartz的默认坐标系统。相对于Quartz来说,这些坐标系统是改动的坐标系统(modified coordinate system)。

UPDATED

在iOS6之后,创建一个AttributedString变成了一件轻松的事情,<CoreText/CoreText.h>已经不须要导入了。假设我要设置字体的颜色,能够直接这样:

  1. [textAttr addAttribute:NSForegroundColorAttributeName
  2. value:[UIColor redColor]
  3. range:NSMakeRange(0, text.length)];

ios NSAttributedString 具体解释的更多相关文章

  1. iOS SDK具体解释之NSCopying协议

    原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn ...

  2. iOS SDK具体解释之UIDevice(系统版本号,设备型号...)

    原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 blog.csdn.net/column/details/huangwenchen ...

  3. iOS Animation具体解释

    iOS Animation具体解释 本篇仅仅要解说iOS中动画的使用. Animtion主要分为两类:UIView动画和CoreAnimation动画. UIView动画有UIView属性动画,UIV ...

  4. iOS --NSAttributedString

    字符属性可以应用于 attributed string 的文本中. 文/iOS_成才录(简书作者) 原文链接:http://www.jianshu.com/p/03a741246737 著作权归作者所 ...

  5. iOS 严重问题解释(crash)

    问题1:Exception Type: 00000020 Exception Codes: 0x000000008badf00d Exception Note: SIMULATED (this is  ...

  6. iOS常识名词解释 2016/04/05

    Bundle : http://www.cnblogs.com/BigPolarBear/archive/2012/03/28/2421802.html http://blog.sina.com.cn ...

  7. 【精】iOS GCD 具体解释

    一.介绍 1.什么是GCD? Grand Central Dispatch.是苹果公司开发的一套多核编程的底层API. GCD首次公布在Mac OS X 10.6,iOS4及以上也可用.GCD存在于l ...

  8. 关于IOS框架的解释

  9. iOS Core Animation具体解释(四)AutoLayout中的动画

    原创blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...

随机推荐

  1. 杭电 5748 Bellovin

    Description Peter has a sequence  and he define a function on the sequence -- , where  is the length ...

  2. 排序算法C语言实现——冒泡、快排、堆排对比

    对冒泡.快排.堆排这3个算法做了验证,结果分析如下: 一.结果分析 时间消耗:快排 < 堆排 < 冒泡. 空间消耗:冒泡O(1) = 堆排O(1) < 快排O(logn)~O(n) ...

  3. 基于链式链表的栈链式存储的C风格实现

    链式链表的头文件与CPP文件见前文 头文件: #ifndef _LINKSTACK_H_ #define _LINKSTACK_H_ typedef void LinkStack; //创建一个栈 L ...

  4. Android开发——Activity启动模式详解

    1. Activity的启动模式 本文原创,转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52054893 1.1 Standard标 ...

  5. spring cloud 学习资源

    1.https://blog.csdn.net/column/details/17737.html 2.https://blog.csdn.net/column/details/15197.html? ...

  6. Oracle跟踪分析数据库启动的各个阶段

    目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...

  7. 大数据学习——hbase的shell客户端基本使用

    1  基本shell命令 1 在hbase的 bin目录下进入命令行 ./hbase shell 2 查看有哪些表 list 3 创建一个表 create 't_user_info', {NAME = ...

  8. install composer on ubuntu14.04

    1.download composer $ sudo apt-get install curl $ curl -sS https://getcomposer.org/installer | php $ ...

  9. [uiautomator篇] python uiautomatorDemo

    #coding=utf-8 """ @version: 1.0 @author: @prerequisite: based on Python 2.7 @usage: 1 ...

  10. EasyUI 获取行ID,符合条件的添加样式

    function GetTableTrID() { var p = $('#PreFixgrid').prev().find('div table:eq(1)'); var ID = $(p).fin ...