ios - set cornerRadius and setbackgroundimage to UIButton -
i trying set cornerradius of uibutton dont know how it.
if this:
button.layer.cornerradius = 5;
works well, if :
button.layer.cornerradius = 5; [button setbackgroundcolor:[uicolor colorwithpatternimage:radialgradient]];
the corners not rounded.
i know solve whit
[button.layer setmaskstobounds:yes];
but looking different solution, because add arrows button , if set mask bounds arrow masked.
edit :
radialgradient made whit func
+ (uiimage *)getradialgradientimage:(cgsize)size centre:(cgpoint)centre radius:(float)radius startcolor:(uicolor *)startcolor endcolor:(uicolor *)endcolor{ // initialise uigraphicsbeginimagecontextwithoptions(size, yes, 1); // create gradient's colours size_t num_locations = 2; cgfloat locations[2] = { 0.0, 1.0 }; const cgfloat *component_first = cgcolorgetcomponents([startcolor cgcolor]); cgfloat red1 = component_first[0]; cgfloat green1 = component_first[1]; cgfloat blue1 = component_first[2]; const cgfloat *component_second = cgcolorgetcomponents([endcolor cgcolor]); cgfloat red2 = component_second[0]; cgfloat green2 = component_second[1]; cgfloat blue2 = component_second[2]; const cgfloat components[8] = { red1,green1,blue1,1,red2,green2,blue2,1}; // end color cgcolorspaceref mycolorspace = cgcolorspacecreatedevicergb(); cggradientref mygradient = cggradientcreatewithcolorcomponents (mycolorspace, components, locations, num_locations); // normalise 0-1 ranged inputs width of image cgpoint mycentrepoint = cgpointmake(centre.x * size.width, centre.y * size.height); float myradius = min(size.width, size.height) * radius; // draw it! cgcontextdrawradialgradient (uigraphicsgetcurrentcontext(), mygradient, mycentrepoint, 0, mycentrepoint, myradius, kcggradientdrawsafterendlocation); // grab autoreleased image uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); // clean cgcolorspacerelease(mycolorspace); // necessary? cggradientrelease(mygradient); // necessary? uigraphicsendimagecontext(); // clean return image; }
here how create button through code , set background color.
uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom]; btn.frame = cgrectmake(100, 100, 100,50); [btn settitle:@"hello" forstate:uicontrolstatenormal]; [btn setbackgroundcolor:[uicolor colorwithred:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]]; btn.frame = cgrectmake(100.0, 100.0, 120.0, 50.0);//width , height should same value btn.clipstobounds = yes; btn.layer.cornerradius = 20;//half of width btn.layer.bordercolor=[uicolor redcolor].cgcolor; btn.layer.borderwidth=2.0f; [self.view addsubview:btn];
below image of button related above code
you can play around code , create colors need background , border. hope out.
Comments
Post a Comment