代码改变世界

UIButton详解

2015-10-20 20:56  xiangjune  阅读(271)  评论(0编辑  收藏  举报

// ----------------------------------UIButton------------------------------

    // UIButtonTypeSystem 点击文字会有效果,变色  UIButtonTypeCustom 无点击效果

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(200, 100, 200, 80);

    [button setTitle:@"click me" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor blueColor];

    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

    // 字体大小

    button.titleLabel.font = [UIFont systemFontOfSize: 20.0];

//    button.font = [UIFont systemFontOfSize:20];  废弃

    //边框

    button.layer.borderWidth = 3;

    button.layer.borderColor = [UIColor grayColor].CGColor;

    // 设置圆角

    [button.layer setMasksToBounds:YES];

    [button.layer setCornerRadius:20.0];

    // 按下会有发光的效果

    button.showsTouchWhenHighlighted = YES;

    

    // 文字居左

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    // 文字居顶

    button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

    

    // 文字内边距

    button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);

    

    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];