在iOS开发中button是必不可少的控件之一, UIButton既可以显示文字又可以显示图片,此处的图片区别于背景图片。UIButton内部有titleLabel和imageView两个控件,默认情况下图片在左边,文本在图片右边显示。下面是button的常规设置:
//设置button上的正常情况下显示的图片
[btn setImage:[[UIImage imageNamed:@“图片名称”]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] forState:UIControlStateNormal];
//设置button上的被点击后显示的图片
[btn setImage:[[UIImage imageNamed:xarr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] forState:UIControlStateSelected];
//设置button大小
btn.frame = CGRectMake(90+i*50, 5, 50, 50);
//设置button方法的实现
[btn addTarget:self action:@selector(Click:)
forControlEvents:UIControlEventTouchUpInside];
//设置button上的正常情况下显示的字体的颜色
btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
[btn setTitle:@“要显示的文字” forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//设置button上的被点击后的显示的字体颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
//设置button上字体的偏移量
[btn setTitleEdgeInsets:UIEdgeInsetsMake(10.0,-40.0 , 0.0, -8.0)];
//设置button上图片的偏移量
[btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 9.0, 25.0, 9.0) ];
//图文互换
self.titleLabel.frame.origin.x = self.imageView.frame.origin.x;
self.imageView.frame.origin.x = CGRectGetMaxX(self.titleLabel.frame);