2012-05-21 41 views
0

我使用CTFramesetterSuggestFrameSizeWithConstraints函数来定义大小,我的属性字符串将需要,但我得到的高度小于它应该是,并且宽度也大于constrainSize.width参数。这是我的代码:CTFramesetterSuggestFrameSizeWithConstraints不限制结果约束大小

CTTextAlignment textAlignment = kCTLeftTextAlignment; 
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping; 

CTParagraphStyleSetting paragraphSettings[] = { 
    { 
     kCTParagraphStyleSpecifierAlignment, 
     sizeof(textAlignment), 
     &textAlignment 
    }, 
    { 
     kCTParagraphStyleSpecifierLineBreakMode, 
     sizeof(lineBreakMode), 
     &lineBreakMode 
    } 
}; 

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, sizeof(paragraphSettings)/sizeof(paragraphSettings[0])); 

NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:[authorString stringByAppendingFormat:@" %@", bodyText]] autorelease]; 

CTFontRef ctfontAuthor = CTFontCreateWithName((CFStringRef)@"Arial-BoldMT", 14.0, NULL); 
CTFontRef ctfontBody = CTFontCreateWithName((CFStringRef)@"Arial", 14.0, NULL); 
[attrString addAttribute:(NSString *)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, [attrString length])]; 
[attrString addAttribute:(NSString *)kCTFontAttributeName value:(id)ctfontAuthor range:NSMakeRange(0, [authorString length] + 1)]; 
[attrString addAttribute:(NSString *)kCTFontAttributeName value:(id)ctfontBody range:NSMakeRange([authorString length] + 1, [bodyText length])]; 
CFRelease(ctfontAuthor); 
CFRelease(ctfontBody); 
CFRelease(paragraphStyle); 

CFRange fitRange; 
CGSize result = CGSizeZero; 
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)attrString); 
result = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [authorString length] + [bodyText length] + 1), NULL, size, &fitRange); 
CFRelease(framesetter); 
return result;// result is the value I need to be constrained to size (229.0, MAXFLOAT) but sometimes it is 231 and some more 

我在做什么错?

回答

0

我有一个类似的问题,我发现从this post解决方案(至少对于高度问题)。你需要指定一个kCTParagraphStyleSpecifierLineSpacingAdjustment。

+0

可能这是正确的另一个案件,但不适合我。我从方法接收的高度与lineHeight值的所需高度不同,但不是行间距值。 –

0

在返回结果大小之前,尝试将result.width参数设置为您的约束大小的宽度。

+0

这不会解决错误文字高度的问题。 –