Characters in a wrapped CATextLayer are partially cut off

1.6k Views Asked by At

I have this code that creates a CATextLayer and draws it onto a 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];
}

Unfortunately, some of the characters are cut off when rendering the view. In the example I posted, the 'L' at the beginning is missing its leftmost pixels, and the 'd' at the end of the second is missing it rightmost pixels.

Is there a way to prevent this cutoff from happening, without changing the way the text is wrapped?

1

There are 1 best solutions below

0
On

Instead of using CATextLayer's default behavior, I wrote a function that takes data from a CATextLayer and manually draw it into a CGContext. I added a parameter for padding to take care of the cutoff issue.

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