不使用系统自带的button
//
// 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);
}
最新文章
- COGS 2532. [HZOI 2016]树之美 树形dp
- Base64 的那些事儿
- mybatis批量插入数据到oracle
- CANopen DS301协议中文翻译V03版
- PHP基础17:日期
- ASP.NET MVC(二) 理解MVC
- 使Web Api 支持跨域资源共享(CORS)
- nefu 115 斐波那契的整除
- Spring IOC bean加载过程
- hibhibernate中hql中的语句where语句查询List出现空
- Gradle的一些技巧和遇到的问题
- 对于jQuery的了解
- mysql一个非常实用解决sql查询优化的函数explain
- js jquery 数组的合并 对象的合并
- 【二分】Base Station Sites @ICPC2017HongKong/upcexam5559
- 安装Mysql报错**此用户已存在!**
- IEnumerable<;T>; 转换为数组
- redis实践:用户注册登录功能
- vs2005 sp1 补丁的安装问题
- 【333】Python3.6 格式化文本
热门文章
- ThinkPHP3.2.3扩展之自动分词获取关键字
- EditPlus 1:更改默认编码方式
- C#三种创建对象方法所需时间比较。。。。。
- .net中实现aspnetpager分页
- iOS - CocoaPods操作详解
- poj3669 广搜
- Table is specified twice, both as a target for &#39;UPDATE&#39; and as a separate source
- OpenCV中IplImage/CvMat/Mat转化关系
- iOS https 证书链获取
- vue-awesome-swiper组件的使用