UIButton image text location, uibutton image text

Source: Internet
Author: User

UIButton image text location, uibutton image text

In the actual development process, text and images are often added on buttons. The positions and images are placed differently as needed. Various display methods are implemented as follows:

 

UIButton + LSAdditions. h

/// UIButton + LSAdditions. h // ZLBiPhone /// Created by xujinzhong on 18/3/14. // Copyright (c) 2018 xujinzhong. all rights reserved. // # import <UIKit/UIKit. h> @ interface UIButton (LSAdditions) // set the background color-(void) setBackgroundColor :( UIColor *) backgroundColor forState :( UIControlState) state; # position of the title of the pragma mark button Image Display // center up and down. The image is on top, and the text is under-(void) verticalCenterImageAndTitle :( CGFloat) spacing;-(void) verticalCenterImageAndTitle; // The default value is 6.0. // In the left-right corner, the text is left, and the image is right-(void) horizontalCenterTitleAndImage :( CGFloat) spacing;-(void) horizontalCenterTitleAndImage; // The default value is 6.0 // In the left-right corner, the image is on the left, and the text is on the right-(void) horizontalCenterImageAndTitle :( CGFloat) spacing;-(void) horizontalCenterImageAndTitle; // The default value is 6.0. // The text is centered. The image is left-(void) horizontalCenterTitleAndImageLeft :( CGFloat) spacing;-(void) horizontalCenterTitleAndImageLeft; // The default value is 6.0, the image is displayed on the right-(void) horizontalCenterTitleAndImageRight :( CGFloat) spacing;-(void) horizontalCenterTitleAndImageRight; // The default value is 6.0 @ end.

UIButton + LSAdditions. m

/// UIButton + LSAdditions. m // ZLBiPhone /// Created by xujinzhong on 18/6/14. // Copyright (c) 2018 xujinzhong. all rights reserved. // # import "UIButton + LSAdditions. h "@ implementation UIButton (LSAddtions)/*** Add button background color ** @ return */-(void) setBackgroundColor :( UIColor *) backgroundColor forState :( UIControlState) state {[self setBackgroundImage: [UIButton imageWithColor: backgroundColor] forState: state] ;}+ (UIImage *) imageWithColor :( UIColor *) color {CGRect rect = CGRectMake (0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext (rect. size); CGContextRef context = UIGraphicsGetCurrentContext (); CGContextSetFillColorWithColor (context, [color CGColor]); CGContextFillRect (context, rect); UIImage * image = Response (); return image;}/*** determine whether to press ** @ return */-(BOOL) isExclusiveTouch {return YES;} # position of the image title displayed by the pragma mark button-(void) verticalCenterImageAndTitle :( CGFloat) spacing {CGSize imageSize = self. imageView. frame. size; CGSize titleSize = self. titleLabel. frame. size; self. titleEdgeInsets = UIEdgeInsetsMake (0.0,-imageSize. width,-(imageSize. height + spacing/2), 0.0); titleSize = self. titleLabel. frame. size; self. imageEdgeInsets = UIEdgeInsetsMake (-(titleSize. height + spacing/2), 0.0, 0.0,-titleSize. width);}-(void) verticalCenterImageAndTitle {const int DEFAULT_SPACING = 6.0f; [self verticalCenterImageAndTitle: DEFAULT_SPACING];}-(void) random :( CGFloat) spacing {CGSize imageSize = self. imageView. frame. size; CGSize titleSize = self. titleLabel. frame. size; self. titleEdgeInsets = UIEdgeInsetsMake (0.0,-imageSize. width, 0.0, imageSize. width + spacing/2); titleSize = self. titleLabel. frame. size; self. imageEdgeInsets = UIEdgeInsetsMake (0.0, titleSize. width + spacing/2, 0.0,-titleSize. width);}-(void) handle {const int DEFAULT_SPACING = 6.0f; [self defined: DEFAULT_SPACING];}-(void) horizontalCenterImageAndTitle :( CGFloat) spacing; {self. titleEdgeInsets = UIEdgeInsetsMake (0.0, 0.0, 0.0,-spacing/2); self. imageEdgeInsets = UIEdgeInsetsMake (0.0,-spacing/2, 0.0, 0.0);}-(void) done; {const int DEFAULT_SPACING = 6.0f; [self horizontalCenterImageAndTitle: DEFAULT_SPACING];} -(void) horizontalCenterTitleAndImageLeft :( CGFloat) spacing {self. imageEdgeInsets = UIEdgeInsetsMake (0.0,-spacing, 0.0, 0.0);}-(void) random {const int DEFAULT_SPACING = 6.0f; [self defined: DEFAULT_SPACING];}-(void) horizontalCenterTitleAndImageRight :( CGFloat) spacing {CGSize imageSize = self. imageView. frame. size; CGSize titleSize = self. titleLabel. frame. size; self. titleEdgeInsets = UIEdgeInsetsMake (0.0,-imageSize. width, 0.0, 0.0); titleSize = self. titleLabel. frame. size; self. imageEdgeInsets = UIEdgeInsetsMake (0.0, titleSize. width + imageSize. width + spacing, 0.0,-titleSize. width);}-(void) horizontalCenterTitleAndImageRight {const int DEFAULT_SPACING = 6.0f; [self horizontalCenterTitleAndImageRight: DEFAULT_SPACING];} @ end

 

The test code is as follows:

#define ktopDistance 50#define kwidth  90#define kheight 50
@ Interface ViewController () @ property (nonatomic, strong) UIButton * btnOne; @ property (nonatomic, strong) UIButton * btnTwo; @ property (nonatomic, strong) UIButton * btnThree; @ property (nonatomic, strong) UIButton * btnFour; @ property (nonatomic, strong) UIButton * btnFive; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor cyanColor]; CGFloat Spacing = 2.f; // center up and down, picture on, text under [self. btnOne verticalCenterImageAndTitle: spacing]; [self. btnOne verticalCenterImageAndTitle]; // The default value is 6.0. // The text is left and the image is right. btnTwo horizontalCenterTitleAndImage: spacing]; [self. btnTwo horizontalCenterTitleAndImage]; // The default value is 6.0. // The picture is left and the text is right. btnThree horizontalCenterImageAndTitle: spacing]; [self. btnThree horizontalCenterImageAndTitle]; // The default value is 6.0. // The text is centered. The image is displayed on the left [se Lf. btnFour horizontalCenterTitleAndImageLeft: spacing]; [self. btnFour horizontalCenterTitleAndImageLeft]; // The default value is 6.0. // The text is centered and the picture is on the right [self. btnFive horizontalCenterTitleAndImageRight: spacing]; [self. btnFive horizontalCenterTitleAndImageRight]; // default 6.0}-(UIButton *) btnOne {if (! _ BtnOne) {_ btnOne = [UIButton new]; _ btnOne. backgroundColor = [UIColor grayColor]; _ btnOne. layer. cornerRadius = 3.f; _ btnOne. layer. masksToBounds = YES; [_ btnOne setTitle: @ "left" forState: UIControlStateNormal]; [_ btnOne setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [_ btnOne setTitleColor: [UIColor brownColor] forState: UIControlStateNormal]; [self. view addSubview: _ btnOne]; [_ BtnOne mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. offset (100); make. centerX. similar to (self. view); make. width. offset (kwidth); make. height. offset (kheight) ;}] ;}return _ btnOne ;}- (UIButton *) btnTwo {if (! _ BtnTwo) {_ btnTwo = [UIButton new]; _ btnTwo. backgroundColor = [UIColor grayColor]; _ btnTwo. layer. cornerRadius = 3.f; _ btnTwo. layer. masksToBounds = YES; [_ btnTwo setTitle: @ "left" forState: UIControlStateNormal]; [_ btnTwo setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [_ btnTwo setTitleColor: [UIColor brownColor] forState: UIControlStateNormal]; [self. view addSubview: _ btnTwo]; [_ BtnTwo mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. similar to (self. btnOne. mas_bottom ). offset (ktopDistance); make. centerX. similar to (self. view); make. width. offset (kwidth); make. height. offset (kheight) ;}] ;}return _ btnTwo ;}- (UIButton *) btnThree {if (! _ BtnThree) {_ btnThree = [UIButton new]; _ btnThree. backgroundColor = [UIColor grayColor]; _ btnThree. layer. cornerRadius = 3.f; _ btnThree. layer. masksToBounds = YES; [_ btnThree setTitle: @ "left" forState: UIControlStateNormal]; [_ btnThree setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [_ btnThree setTitleColor: [UIColor brownColor] forState: UIControlStateNormal]; [self. view addSu Bview: _ btnThree]; [_ btnThree mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. similar to (self. btnTwo. mas_bottom ). offset (ktopDistance); make. centerX. similar to (self. view); make. width. offset (kwidth); make. height. offset (kheight) ;}] ;}return _ btnThree;}-(UIButton *) btnFour {if (! _ BtnFour) {_ btnFour = [UIButton new]; _ btnFour. backgroundColor = [UIColor grayColor]; _ btnFour. layer. cornerRadius = 3.f; _ btnFour. layer. masksToBounds = YES; [_ btnFour setTitle: @ "left" forState: UIControlStateNormal]; [_ btnFour setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [_ btnFour setTitleColor: [UIColor brownColor] forState: UIControlStateNormal]; [self. view addSubview: _ B TnFour]; [_ btnFour mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. similar to (self. btnThree. mas_bottom ). offset (ktopDistance); make. centerX. similar to (self. view); make. width. offset (kwidth); make. height. offset (kheight) ;}] ;}return _ btnFour ;}- (UIButton *) btnFive {if (! _ BtnFive) {_ btnFive = [UIButton new]; _ btnFive. backgroundColor = [UIColor grayColor]; _ btnFive. layer. cornerRadius = 3.f; _ btnFive. layer. masksToBounds = YES; [_ btnFive setTitle: @ "left" forState: UIControlStateNormal]; [_ btnFive setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [_ btnFive setTitleColor: [UIColor brownColor] forState: UIControlStateNormal]; [self. view addSubview: _ btnFive]; [_ btnFive mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. similar to (self. btnFour. mas_bottom ). offset (ktopDistance); make. centerX. similar to (self. view); make. width. offset (kwidth); make. height. offset (kheight) ;}] ;}return _ btnFive ;}@ end

 

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.