Swift rewrites the location of UIButton pictures and captions

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.