UIButton

来源:互联网 发布:网络扑克制作 编辑:程序博客网 时间:2024/06/05 20:50
////  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
原创粉丝点击