+ (uicolor*) Gradientfromcolor: (uicolor*) C1 Tocolor: (uicolor*) c2 withwidth: (cgfloat) Width andheight: (cgfloat) height { Cgsize size = cgsizemake (width, height); Uigraphicsbeginimagecontextwithoptions (size, NO, 0); Cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); nsarray* colors = [Nsarray arraywithobjects: (ID) C1. Cgcolor, (ID) c2. Cgcolor, nil]; Cggradientref gradient = Cggradientcreatewithcolors (ColorSpace, (cfarrayref) colors, NULL); Cgcontextdrawlineargradient (context, gradient, cgpointmake (0, 0), Cgpointmake (Size.width, Size.Height), 0); UIImage *image = Uigraphicsgetimagefromcurrentimagecontext (); Cggradientrelease (gradient); Cgcolorspacerelease (colorspace); Uigraphicsendimagecontext (); return [Uicolor colorwithpatternimage:image];}
For example, the effect of setting a gradient color for a Uilabel
Set Gradient background color
Set a simple font color
iOS adds a gradient color for UIView subclasses