iOS-UIButton-设置button标题和图片位置

一.效果图

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 }
时间: 2024-10-19 10:36:09

iOS-UIButton-设置button标题和图片位置的相关文章

iOS UiButton设置button上的图片和文字共存

在UIbutton中有是三个EdgeInsets的设置:ContentEdgeInsets.titleEdgeInsets.imageEdgeInsets UIEdgeInsetsMake 里面的四个参数表示距离上边界.左边界.下边界.右边界的距离,默认都为零,title/image在button的正中央 left.titleEdgeInsets = UIEdgeInsetsMake(3, -25, 0, 0); left.imageEdgeInsets = UIEdgeInsetsMake(0

iOS UIButton设置图片动画

1.问题描述:实现点击按钮播放MP3音频并开启动画,再点击按钮关闭动画以及音频 效果类似以下(图片来自网络,侵删),动画效果其实就是几张连续的图片组成: 2.实现思路 2.1 自定义view,设置imageview的动画以及添加view的点击手势控制动画播放.结束: 2.2 直接自定义一个button,设置button的imageview属性实现,这样更加简单: 3.实现代码(采用第二种方法) 自定义一个UIbutton,如AnimateImgButton,实现方法 .m //自定义button

iOS UIButton 设置图片文字垂直排列

在实际的iOS项目开发中,我们经常需要改变系统的控件的样式,自己定义一个,同样的当我们发现系统自带的某些方法不好使时,我们也会想到重写这个方法. 本文主要记录笔者设置UIButton图片文字垂直排列的方法,最终解决了在图片和文字垂直排列的情况下,如果文字长度变化会导致图片位置变动的问题,对 于此问题网上有比较多的做法,我就不多说了,在此记录这点细节仅为加深印象并方便以后查阅.如有纰漏还请见谅 方案一:通过调整按钮图片和文字的内边距 UIEdgeInsets typedef struct UIEd

ios开发设置button的选中状态图片

可以在故事版中设置好所需要的图片,然后现在h文件中声明按钮的属性和方法 - (IBAction)moodViewShow:(id)sender; @property (weak, nonatomic) IBOutlet UIButton *faceBtn; 并且与故事版完成连接,然后在按钮的方法里设置选中的状态 - (IBAction)moodViewShow:(id)sender { if ([self.faceBtn isSelected]) {//如果是选中状态就置为no, [self.f

iOS UIButton 设置图片不变型 setImage:

[btn.imageView setContentMode:UIViewContentModeScaleAspectFill]; 下面方法自己可以试下, 你就知效果了... typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. re

ios UIButton设置高亮状态下的背景色

一,通过按钮的事件来设置背景色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - (void)viewDidLoad {     [super viewDidLoad];          UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];     [button1 setTitle:@"button1" 

iOS 动态设置启动图的图片(设置成从服务器请求下来的图片)

公司做一个项目,一开始的启动图是两张,既一张启动图和一张类似于广告图,费力半天劲搞出来了,项目上线之后,客户要求改成一张启动图,但是要是那种从服务器动态获取的图片,我一听傻眼了,还能这么弄吗,启动图不都是固定的吗?怎么设置?我就去百度,去各个技术交流群里咨询.有一位技术大牛告诉我了一个方法,现在分享给大家. 首先,启动图是不能动态获取,要想实现那种效果,只能做出一种假象来,让人家感觉你的启动图就是动态获取的. (1)先把启动图空白或者部分空白(这里如果让启动图空白的话,会十分的难看,所以建议还是

IOS autosizing(设置控件的固定位置大小)

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. /* UIViewAutoresizingNone = 0, 什么都没有设置 UIViewAutoresizingFlexibleLeftMargin = 1 << 0, // 代表距离左边的距离是不确定的, 也就是说距离右边的距离是固定 UIViewAutores

ios中设置button为不能点击且变灰

self.myOverButton.userInteractionEnabled=NO; self.myOverButton.alpha=0.4;