CTFrame剪切第一行文本

时间:2012-10-26 11:27:20

标签: objective-c ios core-text ctframe

我在使用Core Text时遇到了问题,我在CTFrame显示的文本的第一行在顶部被截断,如下面的屏幕截图所示,字符“B” :

Example of CTFrame clipping first line

我认为在设置CTFrame中的前导时我做错了什么。我的代码如下:

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

    NSAttributedString *myString;

    //Create the rectangle into which we'll draw the text
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.bounds);

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

    //Setup leading (line height)
    CGFloat lineHeight = 25;
    CTParagraphStyleSetting settings[] = {
        { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &lineHeight },
        { kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &lineHeight },
    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));
    NSDictionary * attrs = [[NSDictionary alloc] initWithObjectsAndKeys:

                                (__bridge id)CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL) ,
                                (NSString*)kCTFontAttributeName,

                                (id)textColor.CGColor,
                                (NSString*)kCTForegroundColorAttributeName,

                                (__bridge id) paragraphStyle,
                                kCTParagraphStyleAttributeName,
                                nil];

    myString = [[NSAttributedString alloc] initWithString:text attributes:attrs];
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)myString);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,[myString length]), path, NULL);

    CTFrameDraw(frame, context);
    CFRelease(frame);
    CFRelease(framesetter);
    CFRelease(path);
}

其他SO帖子(this onethis one)并没有多大帮助。

如何阻止CTFrame剪切第一行?

- 编辑 -

lineheight缩减为20:

从第二行开始的

lineheight得到尊重,但第一行文本的基线低于顶部的基线。

enter image description here

2 个答案:

答案 0 :(得分:0)

此处CGFloat lineHeight = 25;我确信(从您的屏幕截图中)它不是您当前使用的字体大小(font.pointSize)。 这是剪切文本顶部的唯一原因,因为文本大小不能适合25个高度。

您有两种选择:

1)删除paragraphStyle及其相关代码。按照以下代码替换NSDictionary * attrs = …

NSDictionary * attrs = [[NSDictionary alloc] initWithObjectsAndKeys:
                               (__bridge id)CTFontCreateWithName((__bridge CFStringRef) font.fontName, font.pointSize, NULL) ,
                                (NSString*)kCTFontAttributeName,
                                (id)textColor.CGColor, (NSString*)kCTForegroundColorAttributeName,
                                nil];

CTFrameDraw()会自动处理行高。

2)或者始终提供大于最大字体大小的lineHeight。像lineHeight = font.pointSize + somemargine;

要理解上述说明,请尝试以下方法: 在您当前的代码中设置lineHeight = 20;;您可以看到更多文字将从顶行剪辑,第二行文字将更加封闭到其顶行的底部。

答案 1 :(得分:0)

我设法解决了这个问题。最后它变成了我翻转坐标系的位置,并且通过在代码中更早地翻转它来解决问题。