Swift Lesson Fourth

Source: Internet
Author: User

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

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.