1. first create a new class This class inherits UIButton
2. Implementing several methods
1). Change some properties of the font
-(instancetype) initWithFrame: (cgrect) frame{
self = [superinitwithframe: frame];
if (self) {
1--"Sets the position of the font display (left. right)
self. Titlelabel. TextAlignment =nstextalignmentcenter;
2--"Set the size of the font
Self. Titlelabel. font = [uifontsystemfontofsize:];
3--"Set the color of the font
[ self settitlecolor:[ uicolor colorwithred: 0.33f green: 0.33f blue: 0.33f alpha: 1.00f ] forstate: Uicontrolstatenormal];
4-- "Color
after selected
[ self settitlecolor:[ uicolor colorwithred: 0.32f green: 0.74f blue: 1.00f alpha: 1.00f ] forstate: Uicontrolstateselected];
}
return self;
}
In many cases we set the button to have the text (title) and the picture (image) for the position of the text and the picture is only required to complete such as the following two ways to
2). Set the position of the title , the first two properties set the position of the text, and the last two properties set the size of the text
-(cgrect) Titlerectforcontentrect: (cgrect) contentrect{
return CGRectMake (0,contentrect. size. width,N);
}
3). Set the location of the picture, of course, here to set the font in front of the reference and the scope of the view to set, later can be adjusted according to the detailed scope
-(cgrect) Imagerectforcontentrect: (cgrect) contentrect{
return CGRectMake (Contentrect. size. width -up)/2,2, (+ );
}
This method works very well.
iOS summary (self-defined button)