UIButton

//
//  ButtonViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/15.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "ButtonViewController.h"
#import "MyButton.h"

@interface ButtonViewController ()

@end

// UIButton 常用属性
@implementation ButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self createView:self.view];
    
}

// Button 是通过工厂模式来创建的,他的设置主要是通过方法来实现的
- (void)createView:(id)uiView{
    UIView  *view = (UIView *)uiView;
    // 圆角按钮
    UIButton  *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //    UIButton  *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    //    UIButton  *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
    button.frame = CGRectMake(20, 80, 300, 30);
    button.backgroundColor = [UIColor whiteColor];
    // 设置标题文字,和其中的状态
    [button setTitle:@"正常状态" forState:UIControlStateNormal];
//    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
//    
//    [button setTitle:@"选中状态" forState:UIControlStateSelected];
//    button.selected = YES;
    
    // 设置选中的颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    // 高亮下面的颜色
    [button setTintColor:[UIColor purpleColor]];
    
    // 设置唯一标示
    button.tag = 1;
    
    // 高亮的时候是否发光
    button.showsTouchWhenHighlighted = YES;
    
    // 点击事件
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton  *btn2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn2.frame = CGRectMake(20, 180, 300, 30);
    btn2.backgroundColor = [UIColor whiteColor];
    
    // 自定义按钮
//    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    MyButton *btn3 = [MyButton buttonWithType:UIButtonTypeCustom];

    
    btn3.frame = CGRectMake(20, 250, 300, 150);
    btn3.backgroundColor = [UIColor purpleColor];
    btn3.tag = 2;
    [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    // 设置高亮状态下状态变亮
    btn3.adjustsImageWhenHighlighted = YES; // 只有有图片的时候才会变暗
    
    // 设置背景图片  如果图片会被拉伸,变形了
    [btn3 setBackgroundImage:[UIImage imageNamed:@"tupian.gif"] forState:UIControlStateNormal]; // 点击的时候变灰色
    
    // 设置按钮上的图片(图片不会被拉伸)
    [btn3 setImage:[UIImage imageNamed:@"tupian2.jpg"] forState:UIControlStateNormal];
    
    // 默认图片和文字居中显示, 图片在左边,文字在右边
    [btn3 setTitle:@"卡通1号" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    
    
    [view addSubview:btn3];
    [view addSubview:btn2];

    [view addSubview:button];
}


- (void)btnClick:(id)btn{
    UIButton *button = (UIButton *)btn;
    long tagCount = button.tag;
    if (tagCount == 1) {
        NSLog(@"圆角按钮被点击。。。。。");
    }else if(tagCount == 2){
        NSLog(@"自定义按钮被点击");
    }
    
    

};




@end
//
//  MyButton.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/15.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MyButton.h"

@implementation MyButton

// 重写方法自定义其中的文字和图片的位置,达到自定义的效果

- (CGRect)titleRectForContentRect:(CGRect)contentRect{  // 文字

    return CGRectMake(20, 250, 80, 80);
}


- (CGRect)imageRectForContentRect:(CGRect)contentRect{ // 图片

    return CGRectMake(150, 250, 300, 150);

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值