This article has written a text into a colorful picture of the function, input text to the text into a color text picture, you can choose a different font, gradient, first look at the effect.
Implementation mainly with Cagradientlayer gradient, first look at the upper display implementation code:
-(void) Setupcontentview {UIView *contentview = [[UIView alloc] Initwithframe:cgrectmake (0, ScreenWidth, ScreenHight
-44-300)];
[Self.view Addsubview:contentview]; UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (
Tapgestureaction:)];
[Contentview Addgesturerecognizer:tapgesture];
Self.topcontentview = Contentview;
Self.topContentView.backgroundColor = [Uicolor Clearcolor];
Uilabel *label = [[Uilabel alloc] init];
Label.numberoflines = 0;
Label.text = @ "ABC";
Label.frame = [self calculatecontextlabelframewithtitle:@ ' ABC '];
Label.center = Cgpointmake (CONTENTVIEW.BOUNDS.SIZE.WIDTH/2, CONTENTVIEW.BOUNDS.SIZE.HEIGHT/2);
Label.font = [Uifont fontwithname:self.fontnamearray[0] size:25];
Label.textalignment = Nstextalignmentcenter;
[Contentview Addsubview:label];
Label.backgroundcolor = [Uicolor Clearcolor];
Self.contentlabel = label;
Self.gradientlayer = [Cagradientlayer layer]; Self.gradientLayer.frame =CGRectMake (label.frame.origin.x, LABEL.FRAME.ORIGIN.Y, Label.frame.size.width, label.frame.size.height); Self.gradientLayer.backgroundColor = [Uicolor Clearcolor].
Cgcolor;
Self.gradientLayer.startPoint = Cgpointmake (0,0.5);
Self.gradientLayer.endPoint = Cgpointmake (1,0.5);
Self.gradientLayer.colors = self.grandentarr[0];
[Contentview.layer AddSublayer:self.gradientLayer];
Self.gradientLayer.mask = Self.contentLabel.layer;
Self.contentLabel.frame = Self.gradientLayer.bounds;
}
Recalculates the Self.gradientlayer frame when the input text changes
-(void) Textfieldtextchange: (nsnotification*) notice
{
self.contentLabel.text = Self.textField.text;
[Self recalculategradientlayerframe];
}
Implementation code in the following section:
-(void) Setupbottomview {uiview *bottomview =[[uiview alloc] Initwithframe:cgrectmake (0, ScreenHight-300, ScreenWidth,
300)];
Bottomview.backgroundcolor = Jgcolor (222, 222, 222);
[Self.view Addsubview:bottomview];
Self.bottomcontentview = Bottomview;
UIView *textcontentview = [[UIView alloc] init];
Textcontentview.frame = CGRectMake (0, 0, BottomView.bounds.size.width, 50);
[Bottomview Addsubview:textcontentview];
Textcontentview.backgroundcolor = Jgcolor (55, 44, 16);
Uitextfield *textfield = [[Uitextfield alloc] init];
Textfield.borderstyle = Uitextborderstyleroundedrect;
Textfield.frame = CGRectMake (5, textcontentview.bounds.size.width-20, textcontentview.bounds.size.height-10);
[Textcontentview Addsubview:textfield];
Self.textfield = TextField;
CGFloat orgy = 60;
CGFloat ORGX = 10;
CGFloat space = 10;
CGFloat width = (SCREENWIDTH-ORGX * 2-3 * space)/4;
CGFloat height = 35; for (int i = 0; i < i++) {UIView *VW = [[UIView alloc] InitwitHframe:cgrectmake (ORGX + (i% 4) * width + (i% 4) * spaces, orgy + (I/4) * height + (I/4) * space, width, height)];
Vw.backgroundcolor = [Uicolor Clearcolor];
Vw.tag = i + 1;
[Self.bottomcontentview ADDSUBVIEW:VW]; UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Viewtap:)]
;
[VW Addgesturerecognizer:tapgesture];
Uilabel *label = [[Uilabel alloc] initWithFrame:vw.bounds];
Label.textalignment = Nstextalignmentcenter;
Label.text = @ "ABC";
Label.frame = Vw.bounds;
Label.font = [Uifont fontwithname:self.fontnamearray[i% 4] size:25];
Label.backgroundcolor = [Uicolor Clearcolor];
[VW Addsubview:label];
Cagradientlayer *grandient = [Cagradientlayer layer]; Grandient.frame = CGRectMake (label.frame.origin.x, LABEL.FRAME.ORIGIN.Y, Label.frame.size.width,
Label.frame.size.height); Grandient.backgroundcolor = [Uicolor Clearcolor].
Cgcolor;
Grandient.startpoint = Cgpointmake (0,0.5); Grandient.enDpoint = Cgpointmake (1,0.5);
Grandient.colors = Self.grandentarr[i/4];
[Vw.layer addsublayer:grandient];
Grandient.mask = Label.layer;
Label.frame = Grandient.bounds;
}
}
Code to convert text to a picture:
-(void) gettitleimg {uigraphicsbeginimagecontext (self.topContentView.frame.size);
Cgcontextref context = Uigraphicsgetcurrentcontext (); if ([[[Uidevice currentdevice].systemversion Floatvalue] >= 7.0) {[Self.topcontentview drawviewhierarchyinrect:self
. Topcontentview.frame Afterscreenupdates:yes];
else {[Self.topContentView.layer renderincontext:context];
} uiimage *img = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext (); Cgimageref newimgref = Cgimagecreatewithimageinrect (img. Cgimage, CGRectMake (self.gradientlayer.frame.origin.x, SELF.GRADIENTLAYER.FRAME.ORIGIN.Y + 44,
Self.gradientLayer.frame.size.width, Self.gradientLayer.frame.size.height));
Uigraphicsbeginimagecontextwithoptions (Self.gradientLayer.frame.size, NO, [UIScreen Mainscreen].scale);
context = Uigraphicsgetcurrentcontext ();
CGCONTEXTTRANSLATECTM (context, 0, self.gradientLayer.frame.size.height);
CGCONTEXTSCALECTM (context, 1,-1); Cgcontextdrawimage (Context, CGRectMake (0, 0, Self.gradientLayer.frame.size.width, self.gradientLayer.frame.size.height), newimgref);
UIImage *newimg = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Alassetslibrary *library = [[Alassetslibrary alloc] init]; [Library toolWriteImageToSavedPhotosAlbum:newImg.CGImage Metadata:nil completionblock:^ (nsurl *asseturl, Nserror *
Error {if (error) {Jglog (@ "write error");
} groupname:@ "album name";
}
Core code as above, mainly applied to the Cagradientlayer, screenshots, the method of drawing.
The above is the entire content of this article, I hope to help you learn.