1.NSMutableAttributedString 行高

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];[paragraphStyle setLineSpacing:LINESPACE];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];label.attributedText = attributedString;

http://blog.sina.com.cn/s/blog_9256a1210101ku7o.html

2.boundingRectWithSize 行高

#define UILABEL_LINE_SPACE 6
#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
//给UILabel设置行间距和字间距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];paraStyle.lineBreakMode = NSLineBreakByCharWrapping;paraStyle.alignment = NSTextAlignmentLeft;paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距paraStyle.hyphenationFactor = 1.0;paraStyle.firstLineHeadIndent = 0.0;paraStyle.paragraphSpacingBefore = 0.0;paraStyle.headIndent = 0;paraStyle.tailIndent = 0;//设置字间距 NSKernAttributeName:@1.5fNSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];label.attributedText = attributeStr;
}//计算UILabel的高度(带有行间距的情况)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];paraStyle.lineBreakMode = NSLineBreakByCharWrapping;paraStyle.alignment = NSTextAlignmentLeft;paraStyle.lineSpacing = UILABEL_LINE_SPACE;paraStyle.hyphenationFactor = 1.0;paraStyle.firstLineHeadIndent = 0.0;paraStyle.paragraphSpacingBefore = 0.0;paraStyle.headIndent = 0;paraStyle.tailIndent = 0;NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};CGSize size = [str boundingRectWithSize:CGSizeMake(width, HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;return size.height;
}

http://blog.csdn.net/luco2008/article/details/50977718

3.CTFramesetterSuggestFrameSizeWithConstraints 行高

1)CFMutableAttributedStringRef/NSAttributedString

2)CTParagraphStyleRef/NSMutableParagraphStyle

NSString  *text = @"This\nis\nsome\nmulti-line\nsample\ntext."
UIFont    *uiFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);//  When you create an attributed string the default paragraph style has a leading
//  of 0.0. Create a paragraph style that will set the line adjustment equal to
//  the leading value of the font.
CGFloat leading = uiFont.lineHeight - uiFont.ascender + uiFont.descender;
CTParagraphStyleSetting paragraphSettings[1] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading };CTParagraphStyleRef  paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
CFRange textRange = CFRangeMake(0, text.length);//  Create an empty mutable string big enough to hold our test
CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);//  Inject our text into it
CFAttributedStringReplaceString(string, CFRangeMake(0, 0), (CFStringRef) text);//  Apply our font and line spacing attributes over the span
CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, ctFont);
CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
CFRange fitRange;CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, bounds, &fitRange);CFRelease(framesetter);
CFRelease(string);

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

http://blog.sina.com.cn/s/blog_6308b98c0101byh9.html

4.yytext

http://www.cnblogs.com/lujianwenance/p/5716804.html

5.nsattributedstring 转nsstring

6.UILabel和Scrollview结合用,label高度自适应

https://my.oschina.net/langzhouzhou1/blog/648748

7.计算高度

- (CGFloat)boundingRectWithSize:(CGFloat)widthwithAttributedString:(NSMutableAttributedString *)attrt {CGFloat height = 0;NSStringDrawingOptions options =NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;CGRect rect = [attrt boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)options:optionscontext:nil];height = ceilf(rect.size.height);return height;
}- (CGFloat)stringWidthWithSize:(CGSize)size font:(UIFont *)font {CGFloat width;if ([PAUntils isAtLeastIOS_7_0]) {CGRect rect =[self boundingRectWithSize:sizeoptions:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName : font}context:nil];width = rect.size.width;} else {CGSize strSize = [self sizeWithFont:font constrainedToSize:size];width = strSize.width;}return width;
}

转载于:https://www.cnblogs.com/javastart/p/6097513.html

第2月第24天 coretext 行高相关推荐

  1. CSS行高继承(重点)、覆盖和优先级

    层叠性: 离结构近的起作用,就近原则,只会覆盖起冲突的部分 <style>div {color: red; //被后面的覆盖font-size: 12px; // 和后面没有冲突,不会被覆 ...

  2. 上海房价7月下降24% 创三年最大跌幅

    上海房价7月下降24% 创三年最大跌幅 http://www.sina.com.cn   2008年09月05日 01:57   中国证券网-上海证券报 ⊙本报记者 薛明 上海,这个中国房价的最后堡垒 ...

  3. Web打印连续的表格,自动根据行高分页

    拿到这个需求,我已经蛋碎了一地,经过N天的攻克,终于是把它搞定了,只是不知道会不会在某种情况下出现BUG.表示我心虚没有敢做太多的测试.... ---------------------------- ...

  4. excel 行高 上下留白_拒绝加班,工作中最常用的57个Excel小技巧来了!

    今天高顿君分享的 Excel小技巧,全是工作是最常用且简单易操作的,共57个,希望对同学们有所帮助.(适合版本 Excel2007及以上) 一.文件操作 1.为excel文件添加打开密码 文件 - 信 ...

  5. css文本省略(······)行高错位(bug)- 解决办法

    应用css文本省略(······)属性:-webkit-line-clamp: 3; 导致:行高错位.(F12查看发现css属性line-height的值并没变,但为什么浏览器显示文本的实际行距却变小 ...

  6. easypoi导出一对多,合并单元格,且根据内容自适应行高

    easypoi导出一对多,合并单元格,且根据内容自适应行高 EasyPoi一对多导出 一.pom引入依赖 二.导出实体类 excelPoi常用注解说明 @Excel注解 @ExcelCollectio ...

  7. 基于tabular包的Latex表格尺寸设置方法(列宽和行高)

    基于tabular包的Latex表格尺寸设置方法(列宽和行高) tabel语法的小技巧 设置表格的说明文字时,有的场合要求说明文字在表格下方,有的要求说明文字在表格上方,该怎么调整呢? 只需要把\ca ...

  8. POI Excel行高设置

    1.Excel行高单位概述 px是相对长度,表示pixel,像素,是屏幕上显示数据的最基本的点 pt是绝对长度,表示point,磅,是印刷行业常用单位,等于1/72英寸 DPI(或PPI),分辨率,p ...

  9. html里面行高的原理,CSS行高(line-height)及文本垂直居中原理

    在CS多现业讲进行效通近年有务这行定果过近年有S中,line-height 属性设置两段段文本之间的距离,也就是行高,如果我们把一段文本的line-height设置为父容器的高度就可以实现文本垂直居中 ...

最新文章

  1. 给批量用户设磁盘配额
  2. 关于VC中的stdafx.h
  3. linux上传文件到服务器
  4. .net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(一)
  5. 用C++实现十进制转二进制【个人思想】
  6. 鲲鹏基础软件开发赛道openLooKeng赛题火热报名中,数十万大奖等您来收割
  7. matlablib显示图片
  8. 用yacc编写的算术运算计算器_如何用纯机械实现乘除运算,这是个问题
  9. Oracle 数据库升级
  10. C++:STL之vector,deque对比
  11. invocation, 作者 Medwyn Goodall,女巫医 [搜索 invocation Medwyn Goodall]
  12. python wget_python wget下载文件
  13. hdoj 2122 Ice_cream’s world III【最小生成树】
  14. 信用社pb通用记账_信用社会计记账采用的是()。A、收付实现制B、权责发生制C、借贷记账法D、单式记账法...
  15. 亿图图示--工业自动化模块--啤酒生产处理流程简图和热水冷凝处理架构
  16. 管理类联考-英语 : 前导( 四 )
  17. Java指令-Djava.ext.dirs的陷阱
  18. 唐诗宋词学习·106~110节
  19. 使用离散元法DEM进行转鼓内颗粒动力学研究
  20. NCC通过前台页面查找后台类

热门文章

  1. PHP 开发环境搭建
  2. Yandex – 俄罗斯无限免费空间、免费相册、免费邮箱、免费网盘
  3. 股票模型接口测试需要哪些工具?
  4. Java笔试面试-Java 分布式框架面试总结
  5. OceanBase 二次开发 之 Kunpeng + openEuler 适配优化(一)
  6. android类似苹果健康的app,苹果手表降临 八大健康类app露脸
  7. Matplotlib绘制漂亮的饼状图|python绘制漂亮的饼状图
  8. 怎样才能申请办理工作居住证?需要提交哪些材料?
  9. 1665B Almost Ternary Matrix
  10. Python输入一个英文句子,判断输入的是否是英语