Yossi
Yossi

Reputation: 2535

Draw a thick stroke to a text in label

I am trying to do this:

the needed stroke

so I am using this code:

[self.lblRound setOutlineColor:[UIColor colorWithRed:(68/255.0) green:(82/255.0) blue:(120/255.0) alpha:1]];
 self.lblRound.drawOutline = YES;

but this is what i get:

what i get

I also did try this code:

lblRound.shadowColor = [UIColor colorWithRed:(68/255.0) green:(82/255.0) blue:(120/255.0) alpha:1];
lblRound.shadowOffset = CGSizeMake(0, 0);

I also did try to make another label with the needed color and to draw it behind the text, but if I am playing with the size of the text, it influence on the width of the all text. How can I make the stroke thicker?

I can't use an image because I have a number in this text that should be variable.

Thanks

Upvotes: 1

Views: 627

Answers (1)

Yossi
Yossi

Reputation: 2535

This is the solution:

- (void)drawRect:(CGRect)rect
{
    UIFont *font=[UIFont fontWithName:@"VAGRoundedStd-Bold" size:40];
    CGContextRef context = UIGraphicsGetCurrentContext();

    string = lblRoundnumber;

    CGSize fontWidth = [string sizeWithFont:font];

    CGRect tempRect=rect;
    tempRect.origin.x +=160-(fontWidth.width/2);
    tempRect.origin.y +=high+42;

    CGContextSetRGBStrokeColor(context, 68/255.0f, 82/255.0f, 120/255.0f, 1/1.0f);  
    CGContextSetRGBFillColor(context, 68/255.0f, 82/255.0f, 120/255.0f, 1/1.0f);
    CGContextSetTextDrawingMode(context, kCGTextStroke);
    CGContextSetLineWidth(context, 11);
    [string drawInRect:tempRect withFont:font];

    CGContextSetRGBFillColor(context, 1,1,1,1);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    [string drawInRect:tempRect withFont:font];
}

Upvotes: 2

Related Questions