标签:
一.效果图
1.Button被点击之前
2.Button被点击之后
二.代码
1 - (void)createBtn3 2 { 3 UIImage * buttonImage = [UIImage imageNamed:@"1.jpg"]; 4 UIImage * selectedImage = [UIImage imageNamed:@"2.jpg"]; 5 CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage); 6 CGFloat buttonImageViewHeight = CGImageGetHeight(buttonImage.CGImage); 7 8 NSString * buttonTitle = @"左图右标题"; 9 UIFont * buttonTitleFont = [UIFont systemFontOfSize:17.0f]; 10 NSDictionary * attributes = @{NSFontAttributeName : buttonTitleFont}; 11 CGSize buttonTitleLabelSize = [buttonTitle sizeWithAttributes:attributes]; 12 13 // button宽度,至少为imageView宽度与titleLabel宽度之和 14 CGFloat buttonWith = buttonImageViewWidth + buttonTitleLabelSize.width; 15 // button高度,至少为imageView高度与titleLabel高度之和 16 CGFloat buttonHeight = buttonImageViewHeight + buttonTitleLabelSize.height; 17 18 UIButton * btn3 = [[UIButton alloc] init]; 19 [btn3 setCenter:CGPointMake(160, 160)]; 20 [btn3 setBounds:CGRectMake(0, 0, buttonWith, buttonHeight)]; 21 [btn3 setBackgroundColor:[UIColor redColor]]; 22 23 [btn3.titleLabel setFont:buttonTitleFont]; 24 [btn3 setImage:buttonImage forState:UIControlStateNormal]; 25 26 [btn3 setImage:selectedImage forState:UIControlStateSelected]; 27 28 [btn3 setTitle:buttonTitle forState:UIControlStateNormal]; 29 30 [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 31 32 [btn3.titleLabel setBackgroundColor:[UIColor whiteColor]]; 33 34 [btn3 addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; 35 [self.view addSubview:btn3]; 36 37 } 38 - (void)clicked:(UIButton *)btn3 39 { 40 NSLog(@"btn1被点击了"); 41 NSString * buttonSelectedTitle = @"上图下标题"; 42 [btn3 setTitle:buttonSelectedTitle forState:UIControlStateNormal]; 43 44 CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(btn3.bounds), CGRectGetMidY(btn3.bounds)); 45 46 // 找出imageView最终的center 47 CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(btn3.imageView.bounds)); 48 49 // 找出titleLabel最终的center 50 CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(btn3.bounds)-CGRectGetMidY(btn3.titleLabel.bounds)); 51 52 // 取得imageView最初的center 53 CGPoint startImageViewCenter = btn3.imageView.center; 54 55 // 取得titleLabel最初的center 56 CGPoint startTitleLabelCenter = btn3.titleLabel.center; 57 58 // 设置imageEdgeInsets 59 CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y; 60 61 CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x; 62 63 CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop; 64 65 CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft; 66 67 btn3.imageEdgeInsets = UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight); 68 69 // 设置titleEdgeInsets 70 CGFloat titleEdgeInsetsTop = endTitleLabelCenter.y-startTitleLabelCenter.y; 71 72 CGFloat titleEdgeInsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x; 73 74 CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop; 75 76 CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft; 77 78 btn3.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight); 79 }
标签:
原文地址:http://www.cnblogs.com/mancong/p/5066833.html