29,049
社区成员




NSString* str = @"hello kitty, would you tell me why do you want to go to shanghai?";
//这一句,我要怎么才能知道上面文字分行之后的确切高度,而不是给定一个固定值200?
[str drawInRect:NSMakeRect(10, 20, 100, 200) withAttributes:nil];
[str release];
NSString *labelString = @"hello kitty, would you tell me why do you want to go to shanghai? 真心伤不起\n\
楞个圾有圾和在有了上国有和国因为相关爆发正常情况下,计算行高只需要ascent+descent+leading即可。\n\
在这个略有不同的情况下,leading的值会出现偏差,导致算出来的结果是错误的。如果不管行距,\n\
ascent+descent计算出来的Glyph的高度还是正确的。这样就有了第一步在创建用于绘图的CFAttributedStringRef时,\n\
除了设置字体,多设置一个CTParagraphStyleRef,其中特别应该确定行距kCTParagraphStyleSpecifierLineSpacing。\n\
在计算这里时,先逐行计算ascent+descent,累加起来,再加上一个行数*之前设置好的行距,这样算出来的就是这些文本的实\n\
际高度,CTLineGetTypographicBounds返回的结果是宽度,这样就可得到文本实际填充面积的Rect了";
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]], NSFontAttributeName,
//[NSColor redColor], NSForegroundColorAttributeName,
//[NSColor yellowColor], NSBackgroundColorAttributeName,
nil];
NSSize labelSize = [labelString sizeWithAttributes:attributes];
NSRect labelRect = NSMakeRect(0, 0, labelSize.width, labelSize.height);
[labelString drawInRect:labelRect withAttributes:attributes];
///////////////////////////
hello kitty, would you tell me why do you want to go to shanghai? 真心伤不起
楞个圾有圾和在有了上国有和国因为相关爆发正常情况下,计算行高只需要ascent+descent+leading即可。
在这个略有不同的情况下,leading的值会出现偏差,导致算出来的结果是错误的。如果不管行距,
ascent+descent计算出来的Glyph的高度还是正确的。这样就有了第一步在创建用于绘图的CFAttributedStringRef时,
除了设置字体,多设置一个CTParagraphStyleRef,其中特别应该确定行距kCTParagraphStyleSpecifierLineSpacing。
在计算这里时,先逐行计算ascent+descent,累加起来,再加上一个行数*之前设置好的行距,这样算出来的就是这些文本的实
际高度,CTLineGetTypographicBounds返回的结果是宽度,这样就可得到文本实际填充面积的Rect了
//////////////////////////
没有在200处分行,实事上我需要分行,效果如下,怎么实现呢?
///////////////////////
hello kitty, would you tell me why do you
want to go to shanghai? 真心伤不起
楞个圾有圾和在有了上国有和国因为相关
爆发正常情况下,计算行高只需要ascent+
descent+leading即可。
在这个略有不同的情况下,leading的值会
出现偏差,导致算出来的结果是错误的。如果不
管行距,
ascent+descent计算出来的Glyph的高度
还是正确的。这样就有了第一步在创建用于绘图
的CFAttributedStringRef时,
除了设置字体,多设置一个
CTParagraphStyleRef,其中特别应该确定行距
kCTParagraphStyleSpecifierLineSpacing。
在计算这里时,先逐行计算ascent+
descent,累加起来,再加上一个行数*之前设置
好的行距,这样算出来的就是这些文本的实
际高度,
CTLineGetTypographicBounds返回的结果是宽
度,这样就可得到文本实际填充面积的Rect了
//以下代码没有正确计算出文字的高度...大家看看哪里出错了?
- (int)getAttributedStringHeightWithString:(NSAttributedString*)string WidthValue:(int)width {
int h = 10000; //[string length] * 40;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string); //string为要计算高度的NSAttributedString
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, width, h)); //这里的高要设置足够大
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CGPathRelease(path);
CFRelease(framesetter);
NSArray* linesArray = (NSArray*)CTFrameGetLines(textFrame);
NSUInteger count = [linesArray count];
if(count <= 0) {
CFRelease(textFrame);
return 0;
}
CGPoint origins[count];
CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins);
CGFloat line_y = origins[count - 1].y; //最后一行line的原点y坐标
CGFloat ascent, descent, leading;
CTLineRef line = (CTLineRef)[linesArray objectAtIndex: [linesArray count] - 1];
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGFloat total_height = h - line_y + ascent + descent + leading + 1; //+1为了纠正descent转换成int小数点后舍去的值
CFRelease(textFrame);
return total_height;
}
- (void)drawRect:(NSRect)dirtyRect {
NSString* str = @"hello kitty, would you tell me why do you want to go to shanghai? 真心伤不起\n\
楞个圾有圾和在有了上国有和国因为相关爆发\n正常情况下,计算行高只需要ascent+descent+leading即可。\n\
在这个略有不同的情况下,leading的值会出现偏差,导致算出来的结果是错误的。如果不管行距,\n\
ascent+descent计算出来的Glyph的高度还是正确的。这样就有了第一步在创建用于绘图的CFAttributedStringRef时,\n\
除了设置字体,多设置一个CTParagraphStyleRef,其中特别应该确定行距kCTParagraphStyleSpecifierLineSpacing。\n\
在计算这里时,先逐行计算ascent+descent,累加起来,再加上一个行数*之前设置好的行距,这样算出来的就是这些文本的实\n\
际高度,CTLineGetTypographicBounds返回的结果是宽度,这样就可得到文本实际填充面积的Rect了";
NSAttributedString* string = [[NSAttributedString alloc] initWithString:str];
int h = [self getAttributedStringHeightWithString:string WidthValue:200];
[str drawInRect:NSMakeRect(0, 0, 200, h) withAttributes:nil];
[string release];
[str release];
}