如何计算帧大小以逐行绘制CoreText

时间:2013-03-09 11:40:32

标签: iphone ios objective-c ipad core-text

我正在尝试动态创建基于长NSAttributedString分段的书页。

我现在正在做的是将此类别用于NSAttributedString

@interface NSAttributedString (Height)
- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth;
@end

@implementation NSAttributedString (Height)

- (CGFloat)boundingHeightForWidth:(CGFloat)inWidth
{
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFMutableAttributedStringRef)self); 
    CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(inWidth, 10000), NULL);
    CFRelease(framesetter);
    return suggestedSize.height ;
}
@end

由于我使用CTFramesetter绘制文本,因此工作正常并且正确返回了正确的框高度。 不幸的是现在我需要逐行绘制文本:

-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
//CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

NSArray *lines = (__bridge NSArray *)(CTFrameGetLines(ctFrame));

CFIndex lineCount = [lines count];

for(CFIndex idx = 0; idx < lineCount; idx++)
{
    // For each line found from where it starts and it's length
    CTLineRef line = CFArrayGetValueAtIndex((CFArrayRef)lines, idx);
    CFRange lineStringRange = CTLineGetStringRange(line);
    NSRange lineRange = NSMakeRange(lineStringRange.location, lineStringRange.length);
    // Get the line related string 
    NSString* lineString = [displayedString.string substringWithRange:lineRange];
    // Calculate it's range
    NSRange stringRange = NSMakeRange(lineStringRange.location, lineStringRange.length);

    static const unichar softHypen = 0x00AD;
    // Get the last char of the line
    unichar lastChar = [lineString characterAtIndex:stringRange.length-1];
    // Check if it's a soft hyphenation character
    if(softHypen == lastChar) {
        NSMutableAttributedString* lineAttrString = [[displayedString attributedSubstringFromRange:stringRange] mutableCopy];
        NSRange replaceRange = NSMakeRange(stringRange.length-1, 1);
        // Replace it with an hard hyphenation character
        [lineAttrString replaceCharactersInRange:replaceRange withString:@"-"];

        CTLineRef hyphenLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)lineAttrString);
        CTLineRef justifiedLine = CTLineCreateJustifiedLine(hyphenLine, 1.0, self.frame.size.width);

        CGFloat ascent;
        CGFloat descent;
        // Calculate the line height
        CTLineGetTypographicBounds(justifiedLine, &ascent, &descent, NULL);
        // Set the correct position for the line
        CGContextSetTextPosition(context, 0.0, idx*-(ascent + descent)-ascent);
        CTLineDraw(justifiedLine, context);
    }
    else{
        CGFloat ascent;
        CGFloat descent;
        // Calculate the line height
        CTLineGetTypographicBounds(line, &ascent, &descent, nil);
        CGFloat y = idx*-(ascent + descent)-ascent;
        // Set the correct position for the line
        CGContextSetTextPosition(context, 0.0, y);

        CTLineDraw(line, context);
    }
}

这工作得很好,但并非总是如此。有时会发生这种情况:

Last string cutted out

正如你所看到的那样,最后一行被切断了。 有人有任何建议来解决这个烦人的问题吗?

1 个答案:

答案 0 :(得分:5)

由于某些原因,似乎(ascent + descent)不是一条线的正确高度。事实上,上升+ 1 +下降等于点大小。

只需更改此内容:

CGContextSetTextPosition(context, 0.0, idx*-(ascent + descent)-ascent);

进入这个:

CGContextSetTextPosition(context, 0.0, idx*-(ascent - 1 + descent)-ascent);

做了这个伎俩。 参考:Cocoanetics article about UIFont。 感谢jverrijt提示!