Desmond
Desmond

Reputation: 5001

drawAtPoint not working after converting from CGContextShowTextAtPoint

as CGContextShowTextAtPoint is deprecated in iOS 7, i would like to change CGContextShowTextAtPoint to drawAtPoint but the text is outputting.

 //Add text to UIImage
    -(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
        int w = 32;
        int h = 32;

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

        //char* text= (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
        CGContextSetTextDrawingMode(context, kCGTextFill);
        CGContextSetRGBFillColor(context, 0, 0, 0, 1);

    //    CGContextSelectFont(context, "Montserrat-Regular",12, kCGEncodingMacRoman);
    //    CGContextShowTextAtPoint(context,9,12,text, strlen(text));
    //
        [text1 drawAtPoint:CGPointMake(0, 0) withFont:[UIFont fontWithName:@"Montserrat-Regular" size:12]];

        CGImageRef imgCombined = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
        CGImageRelease(imgCombined);

        return retImage;
    }

Upvotes: 0

Views: 2023

Answers (1)

Desmond
Desmond

Reputation: 5001

ok got it work on

//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
    UIGraphicsBeginImageContext(CGSizeMake(36, 36));
    CGRect aRectangle = CGRectMake(0,0, 36.0f,36.0f);
    [img drawInRect:aRectangle];

    [text1 drawInRect : CGRectMake(0, 10, 36, 36)
             withFont : [UIFont fontWithName:@"Montserrat-Regular" size:12]
        lineBreakMode : NSLineBreakByTruncatingTail
            alignment : NSTextAlignmentCenter ];

    UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;


}

Upvotes: 1

Related Questions