iOS自定义Button如何在按钮中把文字放左边,图片放右边
2015-07-10 13:00
453 查看
废话不说~~这次直接上代码
//
// JSSignupSortButton.m
// JSmaster
//
// Created by jyd on 15/7/10.
// Copyright (c) 2015年 JYD. All rights reserved.
//
#import "JSSignupSortButton.h"
#define JSButtonImageW ScreenWidth/4+10
@implementation JSSignupSortButton
+(instancetype)buttonInstance
{
return [[self
alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
//高亮的时候不要自动调整图标
self.adjustsImageWhenHighlighted =
NO;
self.titleLabel.font = [UIFont
boldSystemFontOfSize:18];
self.imageView.contentMode =
UIViewContentModeLeft;
self.titleLabel.textAlignment =
NSTextAlignmentRight;
[self
setTitleColor:[UIColor
whiteColor] forState:UIControlStateNormal];
}
return
self;
}
-(void)setHighlighted:(BOOL)highlighted
{
}
/**
* 自定义按钮图片的frame
*
* @param contentRect
*
* @return
*/
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageY =
0;
CGFloat imageW =
JSButtonImageW;
CGFloat imageX = contentRect.size.width -
JSButtonImageW;
CGFloat imageH = contentRect.size.height;
return
CGRectMake(imageX, imageY, imageW, imageH);
}
/**
* 自定义按钮标题的frame
*
* @param contentRect
*
* @return
*/
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleY =
0;
CGFloat titleW = contentRect.size.width -
JSButtonImageW;
CGFloat titleX =
0;
CGFloat titleH = contentRect.size.height;
return
CGRectMake(titleX, titleY, titleW, titleH);
}
/**
* 根据Title设定自己的宽度
*
* @param title
* @param state
*/
-(void)setTitle:(NSString *)title forState:(UIControlState)state
{
CGSize titleSize = [title
sizeWithAttributes:@{NSFontAttributeName:
self.titleLabel.font}];
CGFloat titleW = titleSize.width;
CGRect frame =
self.frame;
frame.size.width = titleW +
JSButtonImageW + 5;
self.frame = frame;
[super
setTitle:title forState:state];
}
@end
//
// JSSignupSortButton.m
// JSmaster
//
// Created by jyd on 15/7/10.
// Copyright (c) 2015年 JYD. All rights reserved.
//
#import "JSSignupSortButton.h"
#define JSButtonImageW ScreenWidth/4+10
@implementation JSSignupSortButton
+(instancetype)buttonInstance
{
return [[self
alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
//高亮的时候不要自动调整图标
self.adjustsImageWhenHighlighted =
NO;
self.titleLabel.font = [UIFont
boldSystemFontOfSize:18];
self.imageView.contentMode =
UIViewContentModeLeft;
self.titleLabel.textAlignment =
NSTextAlignmentRight;
[self
setTitleColor:[UIColor
whiteColor] forState:UIControlStateNormal];
}
return
self;
}
-(void)setHighlighted:(BOOL)highlighted
{
}
/**
* 自定义按钮图片的frame
*
* @param contentRect
*
* @return
*/
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageY =
0;
CGFloat imageW =
JSButtonImageW;
CGFloat imageX = contentRect.size.width -
JSButtonImageW;
CGFloat imageH = contentRect.size.height;
return
CGRectMake(imageX, imageY, imageW, imageH);
}
/**
* 自定义按钮标题的frame
*
* @param contentRect
*
* @return
*/
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleY =
0;
CGFloat titleW = contentRect.size.width -
JSButtonImageW;
CGFloat titleX =
0;
CGFloat titleH = contentRect.size.height;
return
CGRectMake(titleX, titleY, titleW, titleH);
}
/**
* 根据Title设定自己的宽度
*
* @param title
* @param state
*/
-(void)setTitle:(NSString *)title forState:(UIControlState)state
{
CGSize titleSize = [title
sizeWithAttributes:@{NSFontAttributeName:
self.titleLabel.font}];
CGFloat titleW = titleSize.width;
CGRect frame =
self.frame;
frame.size.width = titleW +
JSButtonImageW + 5;
self.frame = frame;
[super
setTitle:title forState:state];
}
@end
相关文章推荐
- iOS生成静态库方法-iOS集成静态库-iOS合并静态库
- iOS开发-Xcode Debug、Release、Archive、Profile、Analyze概念解释
- iOS内购详细流程汇总
- 苹果开发 笔记(47) 圆
- ios项目(十七)
- iOS tableviewCell 下划线左边缘对齐
- iOS 屏幕原点坐标 && 导航栏风格的自定义
- IOS之变量和常量
- iOS 本地化遇到的问题
- iOS基础(c)—程序结构的分类
- iOS PAD编写 POPOver
- iOS NSNotification的使用
- 关于IOS的蓝牙(转)
- iOS 设置整理
- ios纯代码NavigationController跳转页面
- 在iOS中创建静态库
- [Xcode 7.0 beta]与[iOS 8.4] Could not find developer disk image
- iOS LBS CoreLocation定位,旋转角度,区域检测,地理编码,框架INTULocationManager使用
- iOS开发-推送通知服务(1)
- iOS单例模式