交换button中图片和按钮的位置
2015-12-07 21:14
288 查看
自定义button中有两个方法:
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
//控制button中label的frame
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
//控制button中imageView的fram
}
contentRect为按钮自己的边框
实现方式为:
#import "CaiZhongButton.h"
@interface
CaiZhongButton()
@property(nonatomic,strong)NSFont *titleFont;
@end
@implementation CaiZhongButton
-(void)awakeFromNib{
self.titleFont=[UIFont
systemFontOfSize:16];
self.titleLabel.font=self.titleFont;
//设置图片居中
self.imageView.contentMode=UIViewContentModeCenter;
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat titleX=0;
CGFloat titleY=0;
//获取button的title文字尺寸
NSDictionary *attrs=@{NSFontAttributeName:self.titleFont};
CGFloat titleW;
if (ios7) {
titleW=[self.currentTitle
boundingRectWithSize:CGSizeMake(MAXFLOAT,
MAXFLOAT)options:NSStringDrawingU
4000
sesLineFragmentOrigin
attributes:attrs context:nil].size.width;
}else{
titleW=[self.currentTitle
sizeWithFont:self.titleFont].width;
}
CGFloat titleH=contentRect.size.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageW=30;
CGFloat imageX=contentRect.size.width-imageW;
CGFloat imageY=0;
CGFloat imageH=contentRect.size.height;
return CGRectMake(imageX, imageY, imageW, imageH);
}
@end
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
//控制button中label的frame
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
//控制button中imageView的fram
}
contentRect为按钮自己的边框
例:
左为系统button的效果 右为自定义的button实现效果实现方式为:
#import "CaiZhongButton.h"
@interface
CaiZhongButton()
@property(nonatomic,strong)NSFont *titleFont;
@end
@implementation CaiZhongButton
-(void)awakeFromNib{
self.titleFont=[UIFont
systemFontOfSize:16];
self.titleLabel.font=self.titleFont;
//设置图片居中
self.imageView.contentMode=UIViewContentModeCenter;
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat titleX=0;
CGFloat titleY=0;
//获取button的title文字尺寸
NSDictionary *attrs=@{NSFontAttributeName:self.titleFont};
CGFloat titleW;
if (ios7) {
titleW=[self.currentTitle
boundingRectWithSize:CGSizeMake(MAXFLOAT,
MAXFLOAT)options:NSStringDrawingU
4000
sesLineFragmentOrigin
attributes:attrs context:nil].size.width;
}else{
titleW=[self.currentTitle
sizeWithFont:self.titleFont].width;
}
CGFloat titleH=contentRect.size.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageW=30;
CGFloat imageX=contentRect.size.width-imageW;
CGFloat imageY=0;
CGFloat imageH=contentRect.size.height;
return CGRectMake(imageX, imageY, imageW, imageH);
}
@end
相关文章推荐
- _mm_packus_epi16
- 分析UIWindow
- [string]字符串中几个比较难的算法和容易搞混的题目
- Continuous Observation Hidden Markov Model with Gaussian Mixture
- spring-boot 测试
- 【java学习笔记s】线程1
- LightOJ 1079 - Just another Robbery (01背包)
- linux下shell显示-bash-4.1#不显示路径解决方法
- Android网络传输框架的核心代码量对比
- UDID和openUDID的区别
- 2、OpenSceneGraph中添加节点、删除节点、隐藏节点以及添加开关的用法
- Codeforces 514 D R2D2 and Droid Army(RMQ+二分法)
- 一些知识点的初步理解_9(独立成分分析-ICA,ing...)
- Java中线程基本知识整理
- 创建控制器的3种方式、深入了解view的创建和加载顺序
- HDU 5592 还原逆序数(树状数组+二分)
- liunx 的安装
- N后问题 Java版
- 深度学习相关链接
- MySQL分区表的管理~2