IOS How to create image from text
You can modify method for change font size or iamge size
+(UIImage *)imageFromText:(NSString *)text
{
CGSize maximumSize = CGSizeMake(300, 1000); //set width for string to wrap.
UIFont *font = [UIFont boldSystemFontOfSize:16];
CGSize strSize = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(strSize);
CGRect newframe = CGRectMake(0, 0, strSize.width, strSize.height);
[text drawInRect:newframe
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
+(UIImage *)imageFromText:(NSString *)text
{
CGSize maximumSize = CGSizeMake(300, 1000); //set width for string to wrap.
UIFont *font = [UIFont boldSystemFontOfSize:16];
CGSize strSize = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(strSize);
CGRect newframe = CGRectMake(0, 0, strSize.width, strSize.height);
[text drawInRect:newframe
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}
Comments
Post a Comment