Vertical center of iOS-Button image and text [center button image and text at the same time], and center ios-Button

Source: Internet
Author: User

Vertical center of iOS-Button image and text [center button image and text at the same time], and center ios-Button

 

In the past, there were not many such requirements, which were often used in recent development. Therefore, we simply encapsulate a Button to vertically center the image and body, and rewrite the layoutSubviews method to achieve this, you can check when the layoutSubviews method is triggered;

-(Instancetype) initWithCoder :( NSCoder *) coder {self = [super initWithCoder: coder]; if (self) {self. layer. shadowColor = [UIColor lightGrayColor]. CGColor; self. layer. shadowOffset = CGSizeMake (0, 1); self. layer. shadowOpacity = 0.1;} return self;} // you can adjust the ratio of this method by yourself-(void) layoutSubviews {[super layoutSubviews]; // calculate the float imgHeight = self. imageView. image. size. height; float imgWidth = self. imageView. image. size. width; // imageView Size self. imageView. frame = CGRectMake (VIEWWIDTH (self)-imgWidth)/2, VIEWHEIGHT (self)/2-imgHeight * 7/7, imgWidth, imgHeight); // titleLabel size [self. titleLabel sizeToFit]; float titleWidth = VIEWWIDTH (self. titleLabel); float titleHeight = VIEWHEIGHT (self. titleLabel); self. titleLabel. frame = CGRectMake (VIEWWIDTH (self)-titleWidth)/2, VIEWHEIGHT (self)/2 + imgHeight/7 + 20 * VIEWHEIGHT (self)/900, titleWidth, titleHeight ); self. titleLabel. textAlignment = NSTextAlignmentCenter ;}

 

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.