Convert IOS text into pictures

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.