第2月第24天 coretext 行高
1.NSMutableAttributedString 行高
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:LINESPACE];//调整行间距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, [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 = ;
paraStyle.tailIndent = ;
//设置字间距 NSKernAttributeName:@1.5f
NSDictionary *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 = ;
paraStyle.tailIndent = ;
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[] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading }; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, );
CFRange textRange = CFRangeMake(, 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(, ), (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)width
withAttributedString:(NSMutableAttributedString *)attrt {
CGFloat height = ;
NSStringDrawingOptions options =
NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
CGRect rect = [attrt boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:options
context: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:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName : font
}
context:nil];
width = rect.size.width;
} else {
CGSize strSize = [self sizeWithFont:font constrainedToSize:size];
width = strSize.width;
} return width;
}
第2月第24天 coretext 行高的更多相关文章
- wpf datagrid row height 行高自动计算使每行行高自适应文本
wpf 的datagrid的行高 要么是Auto,要么是定值:但会带来麻烦就是每行行高都一样. 当需要按内容(主要是wrap 换行的textbox或textblock)来动态调整行高的时候,需要用到d ...
- UITableView!别再用代码计算行高了(一)
你还在用代码去计算行高吗?你不感觉那种方式很low吗?从今天起,试着做些改变吧! 别给我讲你喜欢写代码的感觉,你就是要用代码去计算行高,那我这篇文章不适合你. 在讲解复杂内容之前,还是先学习简单的内容 ...
- Web打印连续的表格,自动根据行高分页
拿到这个需求,我已经蛋碎了一地,经过N天的攻克,终于是把它搞定了,只是不知道会不会在某种情况下出现BUG.表示我心虚没有敢做太多的测试.... ---------------------------- ...
- css行高
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- element UI表格行高、padding等设置报错问题
element UI里面表格的行高需要自己调整高度和设置padding,直接写style是不行的,里面有 : 1.row-style (行的 style) 2.header-row-styl (表 ...
- css中line-height行高的深入学习
之前对css中行高line-height的理解还是有些肤浅,深入后才发觉里面包罗万象.学习行高line-height,首先从基本原理开始 (标注该文章转载 http://www.cnblogs.com ...
- ios label 自动计算行高详解
在OC当中自动计算行高主要调用系统的 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff } span ...
- Xcode8+和iOS10+使用Masonry自动计算行高
说起tableView的自动计算行高,真的是不想再提了,写了不知道几百遍了.可就是这麽一个小玩意儿,把我给难的不行不行的,眼看都要没头发了. 1.设置tableView的预估行高和行高为自动计算 // ...
- bug: 在缓存行高时,总是记录错误.
一,现象: 1.在 cell 中添加了一个 label, 并对 label 设置了 attributeText, 结果滑动的过程中,cell 的高度总是不对,多次出现下一个 cell 覆盖上一个 ce ...
随机推荐
- 【Bootstrap Demo】入门例子创建
本文简单介绍下如何来使用 Bootstrap,通过引入 Bootstrap,来实现一个最基本的入门例子. 在前一篇博文[Bootstrap]1.初识Bootstrap 基础之上,我们完全可以更加方便快 ...
- js实现九九乘法表
<script type="text/javascript"> var sum=0; var wite; for (var i = 1; i < 10; i++) ...
- UVA10048 Audiophobia[Floyd变形]
UVA - 10048 Audiophobia Consider yourself lucky! Consider yourself lucky to be still breathing and h ...
- String,StringBuffer,StringBuilder的区别
public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...
- 2016的ChinaJoy沦为ChinaVR?
China Joy已沦为ChinaVR,厂商烧钱参加? 在上海超过40度的高温天下,游戏爱好者们汗流满面地排起长队,拥挤地通过安检进入场馆,但是很快感受到了一丝凉意. ShowGirl少了 " ...
- 分布式服务框架 dubbo/dubbox 入门示例
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- jQuery旋转木马仿3D效果的图片切换特效代码
用jQuery实现的一款仿3D效果的图片切换特效代码,类似旋转木马一样,幻灯图片以三维视觉上下滑动切换,效果很酷炫,兼容IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜 ...
- 字符编码笔记:ASCII,Unicode和UTF-8 转
本文出处 http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 只是为了记录一下省得要去搜. 今天中午,我突然想搞清楚 ...
- Loogn.OrmLite映射优化记录
大家对ORM效率的争议多半在映射性能方面.自己的ORMLite也是如此,经过前段时间的折腾,已经找不出一个简单的方法再提升一下这部分的方法了.在此把优化涉及的几点记录一下. 注:用于性能测试的Code ...
- 跟我从零基础学习Unity3D开发--初识U3D
首先声明,我也是才开始学,把自己学的记录下来也供一些想要学习的朋友参考,一起努力.希望大家能给我指点一下.切莫喷我. 什么是Unity3d呢? 百度百科------Unity是由Unity Techn ...