UIButton的用法

UIButton的用法

  (2013-06-03 20:22:12)
   
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     
     
       //非公开的按钮风格,改变按钮颜色方法
//       [button setTintColor:[UIColor blueColor]];

       [button setFrame:CGRectMake((320-200)/2, 150, 200, 50)];
       //设置title---------(一些基本属性都可以设置 普通状态 和 高亮选中状态不同显示)
    
       [button setTitle:@"present1233333"   forState:UIControlStateNormal];
       [button setTitle:@"高亮状态" forState:UIControlStateHighlighte d];
     
       NSString *path = [[NSBundle mainBundle] pathForResource:@"ab" ofType:@"png"];
       UIImage *image = [UIImage imageWithContentsOfFile:path];
      
         //设置背景图片或者追加图片---------在按钮中:(1)同时有title,image,追加状态
                                                     //(2)但是image范围太大,会把title挤出去
        
         [button setImage:image forState:UIControlStateNormal] ;
      

      
       [button setTag:102];//设置tag值
      
       //设置背景颜色,图片---------前提是button的type不能为白色圆角矩形,因为太大,button都被白色覆盖了
       button.backgroundColor = [UIColor greenColor];
       [button setBackgroundImage:image forState:UIControlStateHighlighte d];
      
       //设置文字颜色
       [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      
       //设置阴影---颜色,偏移量
       [button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];
       button.titleLabel.shadowOffset = CGSizeMake(2, 0);
        
       //设置文字字号

    button.titleLabel.font = [UIFont fontWithName:@"STHeitiTC-Light" size:18];


    //查找文字名称-先找familyName,再找fontName

       NSArray *familyNames = [UIFont familyNames];
       for(NSString *familyName in familyNames)
       {
               NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
               for(NSString *fontName in fontNames)
               {
                       NSLog(@"fontName = %@",fontName);
               }
       }
      
       //设置文字所处位置,左对齐,右对齐,水平居中对齐。上对齐,下对齐,垂直居中对齐。
       button.contentHorizontalAlignme nt = UIControlContentHorizont alAlignmentLeft;
       button.contentVerticalAlignment  = UIControlContentVertical AlignmentCenter;
      
       //设置文字超过button显示时的显示方式(换行符方式)。6种方式
       //   typedef enum {             
       //    UILineBreakModeWordWrap = 0,                       // Wrap at word boundaries
       //    UILineBreakModeCharacter Wrap,                     // Wrap at character boundaries
       //    UILineBreakModeClip,                                       // Simply clip when it hits the end of the rect
       //    UILineBreakModeHeadTrunc ation,                   // Truncate省略头部,使用..代替
       //    UILineBreakModeTailTrunc ation,                   // Truncate省略尾部,使用..代替
       //    UILineBreakModeMiddleTru ncation,               // Truncate 省略中间,使用..代替
       //    } UILineBreakMode;

       button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      
       //调整按钮的边间距---整个内容的边间距使用:contentEdgeInsets
       //                                 图片使用:imageEdgeInsets属性; 标题使用:titleEdgeInsets
       //如果只设置标题title边距,距离以 标题到图片的距离计算,top = 0;left=30;bottom=0;right=10;
       button.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 10);
      
      
       //如果设置YES,按钮在高亮状态下,图片的颜色会进行自动加深。如果设置成NO,保持原样。default是YES
       //与设置高亮状态,不能一起用。设置高亮了,就不用设置这个了
       //       button.adjustsImageWhenHighligh ted = YES;
      
       //设置按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮

    button.showsTouchWhenHighlighted = YES;


    //设置响应代理方法

       [button addTarget:self action:@selector(popUpViewController:) forControlEvents:UIControlEventTouchUpIns ide];

      
       [self.view addSubview:button];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值