Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stroke with custom width and color #5

Open
mporeh opened this issue Oct 7, 2012 · 2 comments
Open

Stroke with custom width and color #5

mporeh opened this issue Oct 7, 2012 · 2 comments

Comments

@mporeh
Copy link

mporeh commented Oct 7, 2012

hi, i am using your class in a project and i wish to add a stroke,
trying to add the stroke at the end of your drawrect code seems to fail
can you please add stroke functionality?

here is an example for the code that fails me

if (self.strokeColor != nil && self.strokeWidth > 0)
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    CGContextSetTextDrawingMode(context, kCGTextFill);

    // Draw the text without an outline
    [super drawTextInRect:rect];

    CGImageRef alphaMask = NULL;

    // Create a mask from the text (with the gradient)
    alphaMask = CGBitmapContextCreateImage(context);

    // Outline width
    CGContextSetLineWidth(context, strokeWidth);
    CGContextSetLineJoin(context, kCGLineJoinRound);

    // Set the drawing method to stroke
    CGContextSetTextDrawingMode(context, kCGTextStroke);

    // Outline color
    self.textColor = self.strokeColor;

    [self.text drawInRect:textRect withFont:font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];


    //[super drawTextInRect:CGRectMake(rect.origin.x, rect.origin.y+1, rect.size.width, rect.size.height)];

    // Draw the saved image over the outline
    // and invert everything because CoreGraphics works with an inverted coordinate system
    CGContextTranslateCTM(context, 0, textRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, textRect, alphaMask);

    // Clean up because ARC doesnt handle CG
    CGImageRelease(alphaMask);
}
@nicklockwood
Copy link
Owner

I'll investigate this for the next release.

@jjxtra
Copy link

jjxtra commented Aug 8, 2013

Be warned this will be very slow for fonts like Chalkduster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants