FXLabel icon indicating copy to clipboard operation
FXLabel copied to clipboard

Stroke with custom width and color

Open mporeh opened this issue 11 years ago • 2 comments

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

mporeh avatar Oct 07 '12 14:10 mporeh

I'll investigate this for the next release.

nicklockwood avatar Dec 18 '12 17:12 nicklockwood

Be warned this will be very slow for fonts like Chalkduster

jjxtra avatar Aug 08 '13 17:08 jjxtra