1.

  1. NSString *text = @"This\nis\nsome\nmulti-line\nsample\ntext.";
  2. UIFont *uiFont = [UIFont fontWithName:@"Helvetica" size:17.0];
  3. CTFontRef ctFont = CTFontCreateWithName((CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
  4. //
  5. // // When you create an attributed string the default paragraph style has a leading
  6. // // of 0.0. Create a paragraph style that will set the line adjustment equal to
  7. // // the leading value of the font.
  8. // CGFloat leading = uiFont.lineHeight - uiFont.ascender + uiFont.descender;
  9. // CTParagraphStyleSetting paragraphSettings[1] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading };
  10. //
  11. // CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
  12.  
  13. // direction
  14. CTWritingDirection direction = kCTWritingDirectionLeftToRight;
  15. // leading
  16. CGFloat firstLineIndent = 0.0;
  17. CGFloat headIndent = 0.0;
  18. CGFloat tailIndent = 0.0;
  19. CGFloat lineHeightMultiple = 1.0;
  20. CGFloat maxLineHeight = ;
  21. CGFloat minLineHeight = ;
  22. CGFloat paragraphSpacing = 0.0;
  23. CGFloat paragraphSpacingBefore = 0.0;
  24. CTTextAlignment textAlignment = kCTTextAlignmentLeft;//(CTTextAlignment)_textAlignment;
  25. CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;//(CTLineBreakMode)_lineBreakMode;
  26. CGFloat lineSpacing = ;//_lineSpacing;
  27.  
  28. CTParagraphStyleSetting theSettings[] =
  29. {
  30. { kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &textAlignment },
  31. { kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode },
  32. { kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(CTWritingDirection), &direction },
  33. { kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
  34. { kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
  35. { kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineIndent },
  36. { kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent },
  37. { kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIndent },
  38. { kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(CGFloat), &lineHeightMultiple },
  39. { kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &maxLineHeight },
  40. { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &minLineHeight },
  41. { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &paragraphSpacing },
  42. { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBefore }
  43. };
  44.  
  45. CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, sizeof(theSettings) / sizeof(CTParagraphStyleSetting));
  46.  
  47. CFRange textRange = CFRangeMake(, text.length);
  48.  
  49. // Create an empty mutable string big enough to hold our test
  50. CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);
  51.  
  52. // Inject our text into it
  53. CFAttributedStringReplaceString(string, CFRangeMake(, ), (CFStringRef) text);
  54.  
  55. // Apply our font and line spacing attributes over the span
  56. CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, ctFont);
  57. CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);
  58.  
  59. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
  60. CFRange fitRange;
  61.  
  62. CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, self.view.bounds.size, &fitRange);

      CFRelease(paragraphStyle);
      CFRelease(ctFont);

  1. CFRelease(framesetter);
  2. CFRelease(string);

https://github.com/honcheng/RTLabel

https://stackoverflow.com/questions/3374591/ctframesettersuggestframesizewithconstraints-sometimes-returns-incorrect-size

第9月第9天 CTFramesetterCreateWithAttributedString的更多相关文章

  1. 猖獗的假新闻:2017年1月1日起iOS的APP必须使用HTTPS

    一.假新闻如此猖獗 刚才一位老同事 打电话问:我们公司还是用的HTTP,马上就到2017年了,提交AppStore会被拒绝,怎么办? 公司里已经有很多人问过这个问题,回答一下: HTTP还是可以正常提 ...

  2. js获取给定月份的N个月后的日期

    1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...

  3. 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题

    2016 年 12 月 28 日,张小龙在微信公开课 PRO 版的会场上,宣布了微信小程序的正式发布时间. 微信小程序将于 2017 年 1 月 9 号正式上线. 同时他解释称,小程序就像PC时代的网 ...

  4. 【代码笔记】iOS-获得当前的月的天数

    一,代码. #import "ViewController.h" @interface ViewController () @end @implementation ViewCon ...

  5. 怎样两个月完成Udacity Data Analyst Nanodegree

    在迷恋数据科学很久后,我决定要在MOOC网站上拿到一份Data Science的证书.美国三个MOOC网站,Udacity上的课程已经被分成了数个nanodegree,每个nanodegree都是目前 ...

  6. 我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗

    文章背景,回答提问:我想立刻辞职,然后闭关学习编程语言,我给自己3个月时间学习C语言!这样行的通吗? 我的建议是这样:1. 不要辞职.首先说,你对整个开发没有一个简单的了解,或一个系统的入门学习.换句 ...

  7. 【月入41万】Mono For Android中使用百度地图SDK

    借助于Mono For Android技术,.Net开发者也可以使用自己熟悉的C#语言以及.Net来开发Android应用.由于Mono For Android把Android SDK中绝大部分类库都 ...

  8. CS Coder学习asp.net5个月的最大感悟:从http的角度重新认识asp.net(二)——我理解的ajax(二)

    啊哈,时隔两个月,才开始写上一篇文章的后续,实在是惭愧.主要是年尾公司又来活了,忙得团团转,而且这段时间在自学mvc.我在上文中,提到过我对mvc框架的初步印象是:相比webform,算是回归了bs本 ...

  9. C#得到某月最后一天晚上23:59:59和某月第一天00:00:00

    项目需求: 某学校订单截止操作时间的上一个月最后一天晚上23:59:59 为止所有支付的订单统计: 代码: /// <summary> /// 通过学校和截止时间得到订单 /// < ...

随机推荐

  1. HyperLedger Fabric 学习思路分享

    HyperLedger Fabric 学习思路分享 HyperLedger Fabric最初是由Digital Asset和IBM公司贡献的.由Linux基金会主办的一个超级账本项目,它是一个目前非常 ...

  2. 认识 Iconfont 以及什么是 .eot、.woff、.ttf、.svg

    一.Iconfont 1. 概述 在前端作业中,二十年前只有页面中铺满文字就算上线产品,现如今,不加点俏皮的“图标”会让页面显得很 Low 很 Low.   图标 在写这篇文章之前,我一直以为上图中的 ...

  3. Notes of Daily Scrum Meeting(11.12)

    今天我们召开了Beta阶段MOOC项目开发的第一次Scrum Meeting,在会上就alpha阶段进行了总结,然后我们确定了 接下来的Beta阶段的项目分工,并且就每天的进度汇报做了一个约定,就是每 ...

  4. 调研android开发环境的发展演变

    这是第一次接触android开发,特意上网搜索视频进行了自身知识补充,觉得说视频做得很不错,从android的发展历程以及一些基本常识都讲得很详细,也很有趣,也所以拿出来同大家一起分享学习,网址是:h ...

  5. 我是一个程序猿 ——《不是书评 :<我是一只IT小小鸟>》有感

    读了刘未鹏先生的文章<不是书评 :<我是一只IT小小鸟>>,产生了诸多共鸣,更明白了不少道理. 首先是一个很平常的现象,进度条效应,在操作移动终端上的软件时,如果没有进度条,人 ...

  6. 如何向妻子解释OOD

      前言 此文译自CodeProject上<How I explained OOD to my wife>一文,该文章在Top Articles上排名第3,读了之后觉得非常好,就翻译出来, ...

  7. git 生成秘钥

    Permission denied (publickey). fatal: The remote end hung up unexpectedly 应该是ssh key过期了吧 试着重新创建ssh k ...

  8. du 显示 前面以及或者两级的目录大小的方法

    mark 一下 du -h --max-depth=

  9. windows日志查看-非法关机判断方法

    日志文件,它记录着Windows系统及其各种服务运行的每个细节,对增强Windows的稳定和安全性,起着非常重要的作用.但许多用户不注意对它保护,一些“不速之客”很轻易就将日志文件清空,给系统带来严重 ...

  10. blog 社会化评论插件 多说for china, disqus for global range

    1.disqus https://disqus.com/ https://publishers.disqus.com/engage https://disqus.com/admin/create/ h ...