2012-08-01 36 views
2

我有這樣的代碼,創建一個CATextLayer並繪製它放到一個UIView:字符在一個包裹CATextLayer被部分地切斷

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    CGRect viewRect = CGRectMake(50.0f, 50.0f, 345.0f, 120.0f); 

    CATextLayer *textLayer = [CATextLayer layer]; 
    textLayer.contentsScale = [[UIScreen mainScreen] scale]; 
    textLayer.wrapped = YES; 
    textLayer.truncationMode = kCATruncationNone; 
    UIFont *font = [UIFont fontWithName:@"SnellRoundhand" size:20.0f]; 
    textLayer.string = @"Lorem Lipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat."; 
    textLayer.alignmentMode = kCAAlignmentLeft; 
    textLayer.fontSize = font.pointSize; 
    CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, nil); 
    textLayer.font = fontRef; 
    CFRelease(fontRef); 

    textLayer.backgroundColor = [[UIColor lightGrayColor] CGColor]; 
    textLayer.foregroundColor = [[UIColor blackColor] CGColor]; 
    textLayer.frame = viewRect; 


    UIGraphicsBeginImageContextWithOptions(textLayer.frame.size, NO, 0); 
    [textLayer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView *textImageView = [[UIImageView alloc] initWithImage:textImage]; 
    textImageView.frame = viewRect; 
    [self.view addSubview:textImageView]; 
} 

不幸的是,某些字符的呈現視圖時被切斷。在我發佈的例子中,開頭的'L'缺少最左邊的像素,第二個末尾的'd'缺少最右邊的像素。

有沒有辦法來防止這種情況發生,而不改變文本的包裝方式?

回答

1

我沒有使用CATextLayer的默認行爲,而是編寫了一個從CATextLayer獲取數據並手動將其繪製到CGContext中的函數。我添加了一個填充參數來處理截斷問題。

-(UIImage *)imageFromCATextLayer:(CATextLayer *)layer andPaddingSize:(CGSize)paddingSize 
{ 
    CGFloat paddingWidth = paddingSize.width; 
    CGFloat paddingHeight = paddingSize.height; 
    CGRect textBounds = layer.frame; 
    CGRect paddedImageBounds = CGRectMake(0.0f, 0.0f, textBounds.size.width + 2 * paddingWidth, textBounds.size.height + 2 * paddingHeight); 
    UIGraphicsBeginImageContextWithOptions(paddedImageBounds.size, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0.0f, paddedImageBounds.size.height); 
    CGContextScaleCTM(context, 1, -1); 

    CGContextSetFillColorWithColor(context, layer.backgroundColor); 
    CGContextFillRect(context, paddedImageBounds); 

    CTTextAlignment alignment; 
    if ([layer.alignmentMode isEqualToString: kCAAlignmentLeft]) { 
     alignment = kCTLeftTextAlignment; 
    } 
    else if ([layer.alignmentMode isEqualToString: kCAAlignmentCenter]) { 
     alignment = kCTCenterTextAlignment; 
    } 
    else if ([layer.alignmentMode isEqualToString: kCAAlignmentRight]) { 
     alignment = kCTRightTextAlignment; 
    } 
    else { 
     alignment = kCTLeftTextAlignment; 
    } 

    CTParagraphStyleSetting paragraphSettings = {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}; 
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(&paragraphSettings, 1); 

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:layer.font, (NSString*)kCTFontAttributeName, paragraphStyle, kCTParagraphStyleAttributeName, layer.foregroundColor, kCTForegroundColorAttributeName, nil]; 
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:layer.string attributes:attributes]; 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attrString); 
    CGMutablePathRef p = CGPathCreateMutable(); 
    CGPathAddRect(p, NULL, CGRectMake(10.0f, 2 * paddingHeight - 10.0f, textBounds.size.width, textBounds.size.height)); 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), p, NULL); 
    CTFrameDraw(frame, context); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
}