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;

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

      [btn.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
      btn.titleLabel.numberOfLines = 0;  //最大值


背光高亮

      [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



你可能感兴趣的:(IOS_UIKit库)