54.变换UIButton中的label和image的位置
来源:互联网 发布:织梦cms拿站 编辑:程序博客网 时间:2024/06/05 22:31
1.自定义一个button并实现以下方法,在返回frame的方法中拿不到字体大小,一般用myFont属性写死。- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { [self setup]; } return self;}- (void)setup{ // 记录按钮标题的字体 self.myFont = [UIFont systemFontOfSize:15]; // 设置标题的字体 self.titleLabel.font = self.myFont; // 设置按钮的图片显示的内容默认为剧中(为了不拉伸) self.imageView.contentMode = UIViewContentModeCenter;}// 用于返回按钮上标题的位置, 传入按钮的rect- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat titleX = 0; CGFloat titleY = 0; CGFloat titleH = contentRect.size.height; // 获取当前按钮上的文字 //[self titleForState:UIControlStateNormal]; NSString *title = self.currentTitle; CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT); NSMutableDictionary *md = [NSMutableDictionary dictionary]; //md[NSFontAttributeName] = self.titleLabel.font; // 死循环的原因是self.titleLabel需要访问titleLabel, 而self.titleLabel又需要调用当前方法获取title的范围, 所有死循环 NSLog(@"%@", self.myFont); md[NSFontAttributeName] = self.myFont; // 计算文字的范围 CGFloat titleW = 0; // 判断是否是xcode5 , 如果是就编译一下代码, 如果不是就不编译 if (iOS7) { // 是IOS7 CGRect titleRect = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:md context:nil]; titleW = titleRect.size.width; }else { // 非IOS7 CGSize titleSize = [title sizeWithFont:self.myFont]; titleW = titleSize.width; } return CGRectMake(titleX, titleY, titleW, titleH);}- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageY = 0; CGFloat imageH = contentRect.size.height; CGFloat imageW = 16; // 图片的X = 按钮的宽度 - 图片宽度 CGFloat imageX = contentRect.size.width - imageW; return CGRectMake(imageX, imageY, imageW, imageH);}
0 0
- 54.变换UIButton中的label和image的位置
- UIButton 的 image 和title 位置设置
- 自定义UIButton的title和image位置
- UIButton 调整title和image的位置
- [iOS]详解调整UIButton的title和image的位置
- iOS-调整UIButton的title和image的位置
- 详解调整UIButton的title和image的位置
- UIButton上image和title的位置调整
- UIButton通过UIEdgeInsetsMake设置image和title的位置
- iOS 自定义UIButton的title和image位置
- iOS自定义UIButton的Image和Title位置
- 自定义UIButton 图片与label的位置
- UIButton设置textlabel和image位置
- 设置UIButton的image title 位置
- 22.UIButton的标题和图片位置的调整/Image和BackgroundImage
- 调整UIButton中的imageView和titleLabel的相对位置
- UIButton将title和image放置在任意位置上
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- 动态规划-钢条切割
- 移植MonkeyRunner的图片对比和获取子图功能的实现-UiAutomator/Robotium篇
- nbench 使用方法
- 位图法寻找数组中重复出现的数字
- 怒刷leetcode题目(3)226,83,142,86
- 54.变换UIButton中的label和image的位置
- 正则表达式
- spring3 jdbctemplate 注解实例(有时间字段转换的方法) .
- Layer 用法的总结
- svn手动启动
- bzoj1212: [HNOI2004]L语言
- [置顶] Android L SurfaceFlinger dump信息全解(1)
- html5之canvas基础1
- MongoDB:too much data for sort() with no index error异常