Items often encounter buttons that change the position of text and pictures, so try to write a button classification
Import Foundationimport UIKit
/*
Enumerate where the picture is set
*/enumbuttonimageposition:int{ casePositiontop =0 casePositionleft casePositionbottom casePositionright}extension UIButton {
/**
ImageName: the name of the picture
Title:button's Name.
Location of the Type:image
Space: the spacing between picture text
*/func setimageandtitle (imagename:string,title:string,type:buttonimageposition,space space:CGFloat) {se Lf.settitle (title, for:. normal) self.setimage (UIImage (named:imagename), for:. normal) Let Imagewith:cgfloat= (self.imageview?. frame.size.width)!; Let Imageheight:cgfloat= (self.imageview?. frame.size.height)!; var labelwidth:cgfloat=0.0; var labelheight:cgfloat=0.0; Labelwidth= CGFloat (self.titlelabel!. intrinsiccontentsize.width); Labelheight= CGFloat (self.titlelabel!. intrinsiccontentsize.height);
var imageedgeinsets:uiedgeinsets=uiedgeinsets (); var labeledgeinsets:uiedgeinsets=uiedgeinsets (); Switchtype { case . Positiontop:imageedgeinsets= Uiedgeinsetsmake (-labelheight-space/2.0,0,0, -labelwidth); Labeledgeinsets= Uiedgeinsetsmake (0,-imagewith,-imageheight-space/2.0,0); break; case . Positionleft:imageedgeinsets= Uiedgeinsetsmake (0,-space/2.0,0, space/2.0); Labeledgeinsets= Uiedgeinsetsmake (0, space/2.0,0,-space/2.0); break; case . Positionbottom:imageedgeinsets= Uiedgeinsetsmake (0,0,-labelheight-space/2.0, -labelwidth); Labeledgeinsets= Uiedgeinsetsmake (-imageheight-space/2.0,-imagewith,0,0); break; case . Positionright:imageedgeinsets= Uiedgeinsetsmake (0, labelwidth+space/2.0,0,-labelwidth-space/2.0); Labeledgeinsets= Uiedgeinsetsmake (0,-imagewith-space/2.0,0, imagewith+space/2.0); break; } //4. Assigning ValuesSelf.titleedgeinsets =labeledgeinsets; Self.imageedgeinsets=imageedgeinsets; }}
Swift Lesson Fourth