Import UIKitclassResetbtn:uibutton {Let image_ratio:cgfloat=0.7 //the ratio of the picture to the height of the buttonLet title_font:cgfloat= - //set the default size of the button caption font OverrideInit (frame:cgrect) {super.init (frame:frame) Self.setimageandtitle () } Required Init?(coder Adecoder:nscoder) {super.init (Coder:adecoder) Self.setimageandtitle ()} Func Setimageandtitle () {Self.imageview?. Contentmode =. Center//Set the mode center of the picture displaySelf.titlelabel?. TextAlignment =. Center//Text Center DisplaySelf.titlelabel?. Font = uifont.systemfontofsize (Title_font)//text literal default sizeSelf.settitlecolor (Uicolor.lightgraycolor (), forstate:. Normal)//text literal default color } ///overriding the highlighted state Overridevar highlighted:bool {Set{ } Get{ return false } } OverrideFunc Imagerectforcontentrect (Contentrect:cgrect)CGRect {Let imagex:cgfloat=0Let imagey:cgfloat=0Let imagew:cgfloat=self.frame.size.width let Imageh:cgfloat= Self.frame.size.height *Image_ratioreturnCGRectMake (ImageX, Imagey, Imagew, Imageh)}OverrideFunc Titlerectforcontentrect (Contentrect:cgrect)CGRect {Let titlex:cgfloat=0Let titley:cgfloat= Self.frame.size.height *Image_ratio let Titlew:cgfloat=self.frame.size.width let Titleh:cgfloat= Self.frame.size.height * (1.0-image_ratio)returnCGRectMake (Titlex, Titley, Titlew, Titleh)}}
Swift rewrites the location of UIButton pictures and captions