iOS -- UIButton上的文字、图片位置的调整
2016-07-04 09:45
411 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我们都知道UIButton上有titleLable和imageView,但是有时候我们不想用它默认的位置的,想要改变他的文字和图片的位置,那么如何来达到效果呢?</span>
简单思路就是:新建一个类继承于UIButton,重写两个函数,
-(CGRect) imageRectForContentRect:(CGRect)contentRect ; 设置图片的位置
-(CGRect) titleRectForContentRect:(CGRect)contentRect; 设置文字的位置
这样就可以随意设置button上的文字、图片的位置和大小了。
//自定义button的.m文件
//重写父类方法设置按钮图片和文字的位置
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(self.bounds.size.width*7/8, 10, self.bounds.size.width/8 , self.bounds.size.height/2);
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
return CGRectMake(10, 0, self.bounds.size.width/2, self.bounds.size.height);
}
viewcontroller.m内容 :
简单思路就是:新建一个类继承于UIButton,重写两个函数,
-(CGRect) imageRectForContentRect:(CGRect)contentRect ; 设置图片的位置
-(CGRect) titleRectForContentRect:(CGRect)contentRect; 设置文字的位置
这样就可以随意设置button上的文字、图片的位置和大小了。
//自定义button的.m文件
//重写父类方法设置按钮图片和文字的位置
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(self.bounds.size.width*7/8, 10, self.bounds.size.width/8 , self.bounds.size.height/2);
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
return CGRectMake(10, 0, self.bounds.size.width/2, self.bounds.size.height);
}
viewcontroller.m内容 :
- (void)viewDidLoad { [super viewDidLoad]; MeButton *nameBtn = [[MeButton alloc]init]; [self addAButtonWithButton:nameBtn Bytitle:@"张三" Imagename:@"我的"]; nameBtn.sd_layout.centerXEqualToView(self.view).topSpaceToView(self.view,30).heightIs(35).widthRatioToView(self.view,0.9); MeButton *button = [[MeButton alloc]init]; [self addAButtonWithButton:button Bytitle:@"修改密码" Imagename:@"向右"]; button.sd_layout.centerXEqualToView(self.view).topSpaceToView(nameBtn,10).heightIs(35).widthRatioToView(self.view,0.9); } #pragma mark -- 添加button相关方法 -(void)addAButtonWithButton:(UIButton *)button Bytitle:(NSString *)title Imagename:(NSString *)imageName{ [self.view addSubview:button]; [button.layer setMasksToBounds:YES];//设置按钮的圆角半径不会被遮挡 [button.layer setCornerRadius:10]; [button.layer setBorderWidth:1];//设置边界的宽度 //设置按钮的边界颜色 CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0,0,0,1}); [button.layer setBorderColor:color]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setTitle:title forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; }
相关文章推荐
- Leetcode63. Unique Paths II
- Android的UI简要汇总,后期再更新
- confluent环境谨慎删除topic
- 5小时搞定谷歌原生自动化框架UiAutomator1.0
- 解放程序猿(媛)的双手—iOS UI自动化测试
- 解决在Filter中读取Request中的流后,后续controller或restful接口中无法获取流的问题
- UE4问题汇总
- 引入ueditor
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- vuejs学习网站推荐
- OSGI中blueprint简介
- Win10 IoT C#开发 2 - 创建基于XAML的UI程序 及 应用的三种部署方法
- 百度设计负责人在内网道歉了 除了没得到原谅还被除名了
- Sequence算法、思想总结
- UIAlertController_AlertView
- 高性能Key/Value存储引擎levelDB, rocksDB, TiDB,InnoDB
- request的setAttribute()怎么用的?
- StringBuffer和StringBuilder的区别?
- UIScrollView的基本用法
- vue-loader + Webpack 环境搭建