第2月第24天 coretext 行高
Posted lianhuaren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第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(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.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 = 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)width withAttributedString:(NSMutableAttributedString *)attrt { CGFloat height = 0; 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 行高的主要内容,如果未能解决你的问题,请参考以下文章