UIButton的用处特别多,这里只记录下把按钮应用在图文显示的场景,和需要把图片作为按钮的背景图片显示场景;另外记录下在父控件的子控件优先显示方法(控件置于最前面和置于最后面)。先上效果图: 1、当在某个地方既需要显示图片,还需要显示文字,另外还要有点击功能的时候,这时按钮 ...
UIButton的用处特别多,这里只记录下把按钮应用在图文显示的场景,和需要把图片作为按钮的背景图片显示场景;
另外记录下在父控件的子控件优先显示方法(控件置于最前面和置于最后面)。
先上效果图:
1、当在某个地方既需要显示图片,还需要显示文字,另外还要有点击功能的时候,这时按钮是个很好的选择。
按钮中的图片和文字的距离可以自由调整,图片的也可以上下左右翻转。日常项目中像这些场景都是很容易碰到的。
按钮图文设置、图文位置移动、按钮中图片翻转示例代码:
/** 测试图文并茂的按钮,图文移动 */- (void)addMoveImgAndTextButton{ //1、创建一个按钮:30x50 UIButton *iconBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 150, 80)]; [iconBtn setTitle:@"我的好友" forState:UIControlStateNormal]; [iconBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [iconBtn setImage:[UIImage imageNamed:@"pointer"] forState:UIControlStateNormal]; iconBtn.layer.borderColor = [UIColor redColor].CGColor; //边框颜色 iconBtn.layer.borderWidth = 1; //边框宽度 iconBtn.titleLabel.backgroundColor = [UIColor greenColor]; //文字颜色 iconBtn.imageView.backgroundColor = [UIColor blackColor]; //图片颜色 [iconBtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:iconBtn]; self.iconBtn = iconBtn; //2、移动iconBtn按钮图片和文字 UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(170, 100, 120, 45)]; [btn setTitle:@"图右移字左移" forState:UIControlStateNormal]; btn.titleLabel.numberOfLines = 0; [btn setBackgroundColor:[UIColor blackColor]]; [btn addTarget:self action:@selector(changeBtnFrame:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //3、移动iconBtn按钮图片和文字 UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 150, 120, 45)]; [btn2 setTitle:@"字右移图左移" forState:UIControlStateNormal]; btn2.titleLabel.numberOfLines = 0; [btn2 setBackgroundColor:[UIColor blackColor]]; [btn2 addTarget:self action:@selector(changeBtnFrame2:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; //分割线 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 210, 500, 1)]; lineView.backgroundColor = [UIColor grayColor]; [self.view addSubview:lineView];}/** 点击按钮使图片位置翻转 */- (void)clickButton:(UIButton *)sender{ sender.imageView.transform = CGAffineTransformRotate(sender.imageView.transform, M_PI);}/** 移动图片和文字位置 */- (void)changeBtnFrame:(UIButton *)sender{ UIEdgeInsets edge = self.iconBtn.imageEdgeInsets; CGFloat changeNum = 10; self.iconBtn.imageEdgeInsets = UIEdgeInsetsMake(0, edge.left + changeNum, 0, -(edge.left + changeNum)); self.iconBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -(edge.left + changeNum), 0, edge.left + changeNum); NSLog(@"edge.left: %f, edge.right: %f", edge.left, edge.right);}/** 反方向移动图片和文字位置 */- (void)changeBtnFrame2:(UIButton *)sender{ UIEdgeInsets edge = self.iconBtn.imageEdgeInsets; CGFloat changeNum = 10; self.iconBtn.imageEdgeInsets = UIEdgeInsetsMake(0, edge.left - changeNum, 0, -(edge.left - changeNum)); self.iconBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -(edge.left - changeNum), 0, edge.left - changeNum); NSLog(@"...edge.left: %f, edge.right: %f", edge.left, edge.right);}
原标题:记录下UIButton的图文妙用和子控件的优先显示
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。