Convert text into pictures
-(UIImage *) Imagefromtext: (nsarray*) arrcontent Withfont: (cgfloat) fontSize withtextcolor: (Uicolor *) TextColor Withbgimage: (UIImage *) bgimage Withbgcolor: (Uicolor *) BgColor
{
Set the font type and size
Uifont *font = [Uifont fontwithname:@ "Heiti SC" size:fontsize];
Nsmutablearray *arrheight = [[Nsmutablearray alloc] initWithCapacity:arrContent.count];
CGFloat fheight = 0.0f;
For (NSString *scontent in arrcontent) {
Cgsize stringsize = [scontent sizewithfont:font constrainedtosize:cgsizemake (Content_max_width, 10000) LineBreakMode: Nslinebreakbywordwrapping];
[Arrheight Addobject:[nsnumber numberWithFloat:stringSize.height];
Fheight + = Stringsize.height;
}
Cgsize newSize = Cgsizemake (content_max_width+20, fheight+50);
Create a stretchable image for the top of the background and draw it
Uigraphicsbeginimagecontextwithoptions (newsize,no,0.0);
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
If you set a background picture
if (bgimage)
{
uiimage* stretchedtopimage = [Bgimage stretchableimagewithleftcapwidth:0 topcapheight:0];
[Stretchedtopimage drawinrect:cgrectmake (0, 0, Newsize.width, newsize.height)];
}else
{
if (BgColor)
{
Fill background color
[BgColor set];
Uirectfill (CGRectMake (0, 0, newsize.width, newsize.height));
}
}
Cgcontextsetcharacterspacing (CTX, 10);
Cgcontextsettextdrawingmode (CTX, Kcgtextfillclip);
[TextColor set];
int nIndex = 0;
CGFloat fposy = 20.0f;
For (NSString *scontent in arrcontent) {
NSNumber *numheight = [Arrheight objectatindex:nindex];
CGRect rect = CGRectMake (ten, Fposy, Content_max_width, [Numheight Floatvalue]);
[Scontent drawinrect:rect Withfont:font linebreakmode:nslinebreakbywordwrapping Alignment:NSTextAlignmentLeft];
Fposy + = [Numheight floatvalue];
nindex++;
}
Transfer image
UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
return image;
}
Convert IOS text into pictures