UIButton (常用点击按钮)

//圆形Button
beginBtn.layer.cornerRadius = btn_width/2.0;
    beginBtn.layer.masksToBounds = YES;

初始化

    /**
     *  点击按钮的预设效果
     *  UIButtonTypeSystem 系统默认类型 (图片和文字都表现为选中半透明效果,闪现一下)
     *  UIButtonTypeCustom 自定义      (图片表现为加深底色,通常为暗灰; 文字无效果)
     */
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 100, 200,35);
    [btn setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:btn];
    [btn setTitle:@"2221111111111111111111111111111" forState:UIControlStateNormal];


添加点击事件

    [btn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];

-(void)touchBtn:(UIButton*)button
{
    NSLog(@"%s",__FUNCTION__);
    //设置button的Normal属性  和 selected属性后,通过下面简单非语句切换两种状态
    button.selected = !button.selected;
    
}


添加文字阴影

    btn.titleLabel.shadowOffset = CGSizeMake(1, 1);
    [btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];

获取当前状态文字

    @property(nonatomic,readonly,retain) NSString *currentTitle;
    NSLog(@"curren%@",btn.currentTitle);
 

文本位置

    /**
     *  4.文本位置
     *  @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;
     *  how to position content hozontally inside control. default is center
     *
     */
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
      btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

换行(一般自动适配控件大小的换行不用此方法)

     <span style="font-weight: normal;"> [btn.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
      btn.titleLabel.numberOfLines = 0;  //最大值</span>


背光高亮

      [btn setShowsTouchWhenHighlighted:YES];


设置字体大小和类型

// 字体大全 参考:http://www.cnblogs.com/qingjoin/archive/2013/09/24/3337129.html
    btn.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:11]; 
    // 标准宋体
//    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    // 粗体
//    btn.titleLabel.font = [UIFont boldSystemFontOfSize:11];


内容偏移 (top上、left左、buttom下、right右)

    //整体内容偏移20像素
    btn.contentEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
    //图片偏移20像素
//    btn.imageEdgeInsets =  UIEdgeInsetsMake(0, -20, 0, 0);
    //文字偏移20像素
//    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);


枚举

ButtonState
//    enum {
//        UIControlStateNormal       = 0,         常规状态显现             
//        UIControlStateHighlighted  = 1 << 0,    高亮状态显现   
//        UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现
//        UIControlStateSelected     = 1 << 2,    选中状态             
//        UIControlStateApplication  = 0x00FF0000, 当应用程序标志时           
//        UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他            
//    };

ButtonStyle

//    能够定义的button类型有以下6种,
//    typedef enum {
//        UIButtonTypeCustom = 0,          自定义风格 点击文字无变化
//        UIButtonTypeRoundedRect =UIButtonTypeSystem,   已经失效,改为系统
//        UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用
//        UIButtonTypeInfoLight,           亮色感叹号
//        UIButtonTypeInfoDark,            暗色感叹号
//        UIButtonTypeContactAdd,          十字加号按钮
//    } UIButtonType;



cell  button 高亮无效果

OS7解决方法:

viewDidLoad方法中:

[objc]  view plain  copy
  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_0  
  2.     [mTable setDelaysContentTouches:NO];  
  3. #endif  

cellForRowAtIndexPath方法中:

[objc]  view plain  copy
  1. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_0  
  2.         for (id view in cell.subviews) {  
  3.             if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"]) {  
  4.                 UIScrollView *s = (UIScrollView *)view;  
  5.                 s.delaysContentTouches = NO;  
  6.             }  
  7.         }  
  8. #endif  


iOS8解决方法:

viewDidLoad方法中:

tableView.delaysContentTouches = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_0  
    for (UIView *v in mTable.subviews) {  
        if ([v isKindOfClass:[UIScrollView class]]) {  
            ((UIScrollView *)v).delaysContentTouches = NO;  
        }  
    }  
#endif



参考:http://www.68idc.cn/help/buildlang/ask/20150723457388.html



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值