iOS自定义UIButton的实现

来源:互联网 发布:sql server with as 编辑:程序博客网 时间:2024/05/22 03:06

 最近的项目中遇见的自定义的UIButton的情况出现很多种。有的时候觉得要是放几个控件一拼接也可以达到效果。似乎有点太low!!!于是乎就自定义自己的button了。



先看看几个效果

如图1:titleView 和后面一个三角是一个自定义的button



如果2:button是一个横向排列的 图片在左,文字在右

还有一些其他的情况就看自己心情了。(主要看项目需求了)


  看自定义的button如何调用

1:效果图1的代码调用

// 中间按钮    TitleButton *titleButton = [TitleButton titleButton];        [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];    [titleButton setTitle:@"北京有限公司" forState:UIControlStateNormal];    titleButton.frame = CGRectMake(0, 0, 180, 40);    [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];    self.navigationItem.titleView = titleButton;
 2:效果图2的代码调用

SelleButton * btn = [SelleButton selleButton];    btn.frame = CGRectMake(100, 100, 80, 100);    [btn setTitle:@"张三" forState:UIControlStateNormal];    //[btn setBackgroundImage:[UIImage imageNamed:@"2bj"] forState:UIControlStateHighlighted];    [self.view addSubview:btn];


 


 看代码如何实现:

 1:vc的.h文件如下

   

#import <UIKit/UIKit.h>@interface MarkButton : UIButton+ (instancetype)markButton;@end

2:vc的.m文件如下

#define MarkButtonTitleW 40#import "MarkButton.h"@implementation MarkButton+ (instancetype)markButton{    return [[self alloc] init];}- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // 高亮的时候不要自动调整图标        self.adjustsImageWhenHighlighted = NO;        self.titleLabel.font = [UIFont boldSystemFontOfSize:15];        //self.titleLabel.textColor = [UIColor whiteColor];        self.imageView.contentMode = UIViewContentModeCenter;        self.titleLabel.textAlignment = NSTextAlignmentCenter;        // 背景        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    }    return self;}- (CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageY = 0;    CGFloat imageX = 0;    CGFloat imageW = contentRect.size.width - MarkButtonTitleW;    CGFloat imageH = contentRect.size.height;    return CGRectMake(imageX, imageY, imageW, imageH);}- (CGRect)titleRectForContentRect:(CGRect)contentRect{    CGFloat titleY = 0;    CGFloat titleW = MarkButtonTitleW;    CGFloat titleX = contentRect.size.width - titleW;    CGFloat titleH = contentRect.size.height;    return CGRectMake(titleX, titleY, titleW, titleH);}@end


3:详细介绍如下

查阅api系统官方给我我们这几个方法

- (CGRect)backgroundRectForBounds:(CGRect)bounds;- (CGRect)contentRectForBounds:(CGRect)bounds;- (CGRect)titleRectForContentRect:(CGRect)contentRect;- (CGRect)imageRectForContentRect:(CGRect)contentRect;



  3.1重写Button的Title

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

这里的contentRect就是你当前在使用自定义Button的title 的width和height。我们重写这个方法,可以让button的title的位置变换位置和改变大小。

  3.2 重写Button的Image

- (CGRect)imageRectForContentRect:(CGRect)contentRect;
这里的contentRect就是你当前在使用自定义Button的image的width和height。我们重写这个方法,可以让button的image的位置变换和改变大小。


 3.3 剩下的两个方法就看各位了。

(提高装逼能力!!!)


添加微信公众号最快最好的收听


0 0
原创粉丝点击