2012-10-26 46 views
3

我遇到的問題,而使用核心文本,其中,在CTFrame我顯示文本的第一行的頂部被切斷時,如在下面的截圖看到的第一行中,以字符「 B「:CTFrame剪切文本

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從第二行起被推崇,但是文本的第一行的基線是小於20的下方頂端。

enter image description here

回答

0

我設法解決這個問題。最後它變成了我翻轉座標系的位置,並且通過在代碼中提前翻轉它,問題得到修復。

+0

我注意到你幾年前得到了你的解決方案。但是,你能幫我詳細說明一下「更早地翻轉它」是什麼意思嗎?因爲從我在問題部分通過你的代碼中讀取的內容,一旦創建了'CGMutablePathRef',它就直接進行翻動。 – Willy

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;;你可以看到更多的文字將從頂行剪切,第二行文字將更靠近頂行的底部。

+0

我原本以爲這樣,但從頂部到底部的測量顯示,第一行遠低於指定的'lineheight',而第二行則是完全按照指定的。請參閱問題中的編輯。 –

+0

第二行和第三行使用兩行之間的標準空格。如果你的行高過低(例如10),你也可以看到它們的剪輯。 做一兩件事,註釋掉(__bridge ID)paragraphStyle,kCTParagraphStyleAttributeName, 並嘗試無行間距和paragraphStyle。 – atrane