//

//  LKTitleBtn.m

//  01-彩票

//

//  Created by Lenny  on 3/17/15.

//  Copyright (c) 2015 Lenny. All rights reserved.

//

#import "LKTitleBtn.h"

#import <Availability.h>

@interface LKTitleBtn ()

@property(nonatomic,strong) UIFont *
myFont;

@end

@implementation LKTitleBtn

-(id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super initWithCoder:aDecoder])

{

[self setup];

}

return self;

}

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])
{

[self setup];

}

return self;

}

-(void)setup

{

//    记录button标题的字体

self.myFont = [UIFont systemFontOfSize:15];

//    设置标题的字体

self.titleLabel.font = self.myFont;

//    设置button的图片显示的内容默觉得拉伸不是居中

self.imageView.contentMode = UIViewContentModeCenter;

}

//用于返回button上标题的位置,传入button的rect

-(CGRect)titleRectForContentRect:(CGRect)contentRect

{

CGFloat titleX = 0;

CGFloat titleY = 0;

CGFloat titleW = 0;

CGFloat titleH = contentRect.size.height;

//    获取button上的字体 1

[self titleForState:UIControlStateNormal];

//    获取button上的字体2

NSString * title = self.currentTitle;//建议使用这种方法 这种方法获得的是不论什么状态下的title

CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);

NSMutableDictionary * md = [NSMutableDictionary dictionary];

// 死循环的原因是self.titleLabel须要訪问titleLabel, 而self.titleLabel又须要调用当前方法获取title的范围, 全部死循环

//    md[NSFontAttributeName] = self.titleLabel.font;

//    NSLog(@"%@", self.myFont);

md[NSFontAttributeName] = self.myFont;

//    计算文字的范围

// 推断是否是xcode5 , 假设是就编译一下代码, 假设不是就不编译

#ifdef __IPHONE_7_0

if (([[UIDevice currentDevice].systemVersion doubleValue]
>= 7.0)) {

CGRect titleRect = [title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:md context:nil];

titleW = titleRect.size.width;

}else{

CGSize titleSize = [title sizeWithFont:self.myFont];//过时的方法

titleW  = titleSize.width;

}

#else

// XCODE4

CGSize titleSize = [title sizeWithFont:self.myFont];

titleW = titleSize.width;

#endif

return CGRectMake(titleX, titleY, titleW, titleH);

}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{

CGFloat imageY = 0;

CGFloat imageH = contentRect.size.height;

CGFloat imageW = 16;//图片的宽度

//    图片的X = button的宽度 - 图片的宽度

CGFloat imageX= contentRect.size.width -
imageW;

return CGRectMake(imageX,
imageY, imageW, imageH);

}

最新文章

  1. COGS 2532. [HZOI 2016]树之美 树形dp
  2. Base64 的那些事儿
  3. mybatis批量插入数据到oracle
  4. CANopen DS301协议中文翻译V03版
  5. PHP基础17:日期
  6. ASP.NET MVC(二) 理解MVC
  7. 使Web Api 支持跨域资源共享(CORS)
  8. nefu 115 斐波那契的整除
  9. Spring IOC bean加载过程
  10. hibhibernate中hql中的语句where语句查询List出现空
  11. Gradle的一些技巧和遇到的问题
  12. 对于jQuery的了解
  13. mysql一个非常实用解决sql查询优化的函数explain
  14. js jquery 数组的合并 对象的合并
  15. 【二分】Base Station Sites @ICPC2017HongKong/upcexam5559
  16. 安装Mysql报错**此用户已存在!**
  17. IEnumerable&lt;T&gt; 转换为数组
  18. redis实践:用户注册登录功能
  19. vs2005 sp1 补丁的安装问题
  20. 【333】Python3.6 格式化文本

热门文章

  1. ThinkPHP3.2.3扩展之自动分词获取关键字
  2. EditPlus 1:更改默认编码方式
  3. C#三种创建对象方法所需时间比较。。。。。
  4. .net中实现aspnetpager分页
  5. iOS - CocoaPods操作详解
  6. poj3669 广搜
  7. Table is specified twice, both as a target for &#39;UPDATE&#39; and as a separate source
  8. OpenCV中IplImage/CvMat/Mat转化关系
  9. iOS https 证书链获取
  10. vue-awesome-swiper组件的使用