2012-08-01 1 views
2

나는 CATextLayer를 만들고있는 UIView에 그것을 그리는 코드가 있습니다보기를 렌더링 할 때 문자가 부분적으로 차단하는

- (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; 
}