Quang Phạm Công
Quang Phạm Công

Reputation: 253

Set stroke width for text when draw to image

I found a function to create image from text with color of text, color of stroke, and width of the stroke I can set color of text and color of stroke with CGContextSetRGBFillColor, and CGContextSetRGBStrokeColor, but i don't know how to set width for the stroke of text. Please help me solve this problem. Here is my sample code, thanks for your help.

-(UIImage *)imageFromText:(NSString *)text withFont: (NSString *)fontName{
       // set the font type and size
       UIFont *font = [UIFont fontWithName:fontName size:100];
       CGSize size  = [text sizeWithFont:font];
       CGSize newSize = CGSizeMake(size.width+30, size.height+20);
       // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
       if (UIGraphicsBeginImageContextWithOptions != NULL)
            UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
       else
       // iOS is < 4.0
            UIGraphicsBeginImageContext(newSize);
       CGContextRef ctx = UIGraphicsGetCurrentContext();
       CGContextSetCharacterSpacing(ctx, 10);
       CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
       CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6
       CGContextSetRGBStrokeColor (ctx, 0.2, 0.8, 0.6, 1);

       // draw in context, you can use also drawInRect:withFont:
       [text drawAtPoint:CGPointMake(15.0, 10.0) withFont:font];

       // transfer image
       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();

       return image;

 }

Upvotes: 0

Views: 643

Answers (1)

Nookaraju
Nookaraju

Reputation: 1668

this may help you.

NSString *str= @"YOUR String";
UIFont *font = [UIFont systemFontOfSize:16.0];
CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(301, 10000) lineBreakMode:UILineBreakModeWordWrap];
CGRect Rect = CGRectMake(0, 0, stringSize.width , stringSize.height);
[str drawInRect:Rect withFont:[UIFont systemFontOfSize:16] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];

Upvotes: 1

Related Questions