zoukankan      html  css  js  c++  java
  • IOS--UIButton的使用方法

     

    设置UIButton的文字显示位置、字体的大小、字体的颜色

    分类: iphone界面详解

    btn.frame = CGRectMake(x, y, width, height);

    [btn setTitle: @"search" forState: UIControlStateNormal];

    //设置按钮上的自体的大小

    //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

    //应该使用

    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

    [btn seBackgroundColor: [UIColor blueColor]];

    //最后将按钮加入到指定视图superView

    [superView addSubview: btn];

    ==========================================================

    tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

    这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

    [btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字

    有些时候我们想让UIButton的title居左对齐,我们设置

    btn.textLabel.textAlignment = UITextAlignmentLeft

    是没有作用的,我们需要设置

    btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

    但是问题又出来,此时文字会紧贴到做边框,我们可以设置

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

    使文字距离做边框保持10个像素的距离。

    =======================================================

    设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

    [btn.titleLabel setTextColor:[UIColorblackColor]];

    btn.titleLabel.textColor=[UIColor redColor];

    而是用:

    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

     

     

     

     

     

    IOS--UIButton的使用方法详细

     (2013-08-23 17:20:38)

       // UIButton的常用方法

        UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom]; // 初始化时设置Button样式

        // 风格有如下

    //    typedef enum {

    //        UIButtonTypeCustom = 0,           // 自定义,无风格

    //        UIButtonTypeRoundedRect,        // 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片

    //        UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁

    //        UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁

    //        UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮

    //        UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁

    //    } UIButtonType;

        

        

        // 最常用

        oneButton.frame = CGRectMake(10, 10, 300, 30); // 设置oneButton的位置和大小

        oneButton.backgroundColor = [UIColor blueColor]; // 设置oneButton背景色

        oneButton.alpha = 0.8; // 设置oneButton的透明度范围在0.0-1.0之间

        [oneButton setTitle:@"我是一个UIButton" forState:UIControlStateNormal]; // 设置在什么状态下显示什么文字

        [oneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// 设置什么状态下字体什么颜色

        [oneButton setBackgroundImage:[UIImage imageNamed:@"image.jpg"]forState:UIControlStateNormal]; // 设置什么状态下显示的背景图像

        // 上面几个方法都提到 共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

    //    enum {

    //        UIControlStateNormal       = 0,//常态

    //        UIControlStateHighlighted  = 1 << 0,    //  高亮

    //        UIControlStateDisabled     = 1 << 1,    //禁用

    //        UIControlStateSelected     = 1 << 2,    // 选中

    //        UIControlStateApplication  = 0x00FF0000,// 当应用程序标志使用时

    //        UIControlStateReserved     = 0xFF000000 // 为内部框架预留的

    //    };

        oneButton.tag = 10001; // 设置标签,用于便于点击的是哪个按钮,常用在委托方法中

        [oneButton.titleLabel setFont:[UIFont systemFontOfSize:25.0f]]; // 设置文字的大小

        

        oneButton.adjustsImageWhenDisabled = NO; // 对按钮进行微调。当按钮禁用时,图像会被画的颜色深一些 默认是YES,禁止设置为NO

        oneButton.adjustsImageWhenHighlighted = NO; // 对按钮进行微调。当按钮高亮是,图像会被画得颜色深一些 默认是YES,禁止设置为NO

        

        oneButton.showsTouchWhenHighlighted = YES; // 设置点击的时候是否有光照得效果

        

        // 给按钮添加响应事件

        [oneButton addTarget:self action:@selector(oneButtonTouchUpInside:)forControlEvents:UIControlEventTouchUpInside];//当按钮被按下时会执行oneButtonTouchUpinside:方法。这个方法是自定义的,需要自己写出。参数是(UIButton *)类型

        

        // 添加到view

        [self.view addSubview:oneButton];

    UIButton是一个标准的UIControl控件,所以如果你对UIControl不甚了解还是先看一下我的另一篇博文:《UIControl IOS控件编程》

    一、创建

    两种方法:

    1. 常规的 initWithFrame

    C代码  收藏代码
    1. UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];  
     

    对代码创建View(UIControl继承自UIView,所以也是view)不甚了解的请参看:《有关View的几个基础知识点》

    2. UIButton 的一个类方法(也可以说是静态方法)buttonWithType

    C代码  收藏代码
    1. UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
     

    风格有如下

    C代码  收藏代码
    1. typedef enum {  
    2.     UIButtonTypeCustom = 0,           // no button type   自定义,无风格  
    3.     UIButtonTypeRoundedRect,          // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片  
    4.     UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁  
    5.     UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁  
    6.     UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮  
    7.     UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁  
    8. } UIButtonType;  
      

    二、设置属性

    1.Frame属性

    第2种方法创建按钮后你可以给按钮的frame属性赋值,用一个CGRect结构设置他的位置和大小

    C代码  收藏代码
    1. CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0);  
    2.     btn2.frame =btn2Frame;  
      

    2. title属性

    对于任何特定状态下的按钮,都可以设定该按钮该状态下的按钮标题。用setTitle 方法 设置即可:

    C代码  收藏代码
    1. [btn1 setTitle:@"BTN1" forState:UIControlStateNormal];  
     

    你也可以为按钮的某一状态设置为图。用 setImage 即可:

    C代码  收藏代码
    1. [btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];  
      

    此外,你还可以为每种按钮状态设置标题的颜色和阴影,以及按钮的背景。方法 setTitleColor 和 setTitleShadowColor 都需要一个UIColor对象做参数:

    C代码  收藏代码
    1. [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//设置标题颜色  
    2.     [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//阴影  
    3.     [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景图像  
     

    上面几个方法都提到 共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

    C代码  收藏代码
    1. enum {  
    2.     UIControlStateNormal       = 0,  //常态                       
    3.     UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set 高亮  
    4.     UIControlStateDisabled     = 1 << 1,  //禁用  
    5.     UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below) 选中  
    6.     UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use 当应用程序标志使用时  
    7.     UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use  为内部框架预留的  
    8. };  
    9. typedef NSUInteger UIControlState;  
     

    你只要掌握前四种状态就好了。

    当按钮高亮或者禁用,UIButton 类可以调整自己的外观,下面几个属性可以让你按照需要对按钮的外观进行微调:

    adjustsImageWhenHighlighted

    默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,请将这个属性设置为NO:

    C代码  收藏代码
    1. btn1.adjustsImageWhenHighlighted = NO;  
     

    adjustsImageWhenDisabled

    默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,请将这个属性设置为NO:

    C代码  收藏代码
    1. btn1.adjustsImageWhenDisabled = NO;  
     

    showsTouchWhenHighlighted

    这个属性设置为YES,可令按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮:

    C代码  收藏代码
    1. btn1.showsTouchWhenHighlighted = YES;  
     

    三、显示控件

     显示控件一如继往的简单:

    C代码  收藏代码
    1. [self.view addSubview:btn1];  
    2.     [self.view addSubview:btn2];  
     

    四、重写绘制行为

    你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。

    注意:不要直接调用这些方法, 这些方法是你写给系统调用的。

    C代码  收藏代码
    1. backgroundRectForBounds   //指定背景边界  
    C代码  收藏代码
    1. contentRectForBounds // 指定内容边界  
    C代码  收藏代码
    1. titleRectForContentRect    // 指定文字标题边界  
    C代码  收藏代码
    1. imageRectForContentRect     //指定按钮图像边界  
        

    例:

    C代码  收藏代码
    1. - (CGRect)imageRectForContentRect:(CGRect)bounds{  
    2.      return CGRectMake(0.0, 0.0, 44, 44);  
    3.  }  
     

    五、添加动作

    按钮是用来干嘛的?用来激发某个动作或事件的。那我们我们要为他添加一个动作,与 UIControl 里讲的一样:

    C代码  收藏代码
    1. -(void)btnPressed:(id)sender{  
    2.     UIButton* btn = (UIButton*)sender;  
    3.     //开始写你自己的动作  
    4. }  
    5.  [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];  
     

    六、END

    END?还未结束,不过UIButton结束了,留下一个未结束的话题: UIBarButtonItem .他和UIButton啥关系,后面会有文章来讲,目前你只要搞清楚UIButton就好了。

    一个写此文用的小Demo附在后面:UIButtonDemo

     //放置按钮

        //登录

        UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];

        loginButton.frame = CGRectMake(30, 250, 60, 30);

        [loginButton setTitle:@"登录" forState:UIControlStateNormal];

        //添加事件

        [loginButton addTarget:self action:@selector(loginAction:)

              forControlEvents:UIControlEventTouchUpInside];

        [_loginView addSubview:loginButton];

        

        //注册

        UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];

        registerButton.frame = CGRectMake(120, 250, 60, 30);

        [registerButton setTitle:@"注册" forState:UIControlStateNormal];

        // [loginButton addTarget:self action:@selector(login:)

        //       forControlEvents:UIControlEventTouchUpInside];

        [_loginView addSubview:registerButton];

        

        //密码找回

        UIButton *regetButton = [UIButton buttonWithType:UIButtonTypeSystem];

        regetButton.frame = CGRectMake(220, 250, 60, 30);

        [regetButton setTitle:@"密码找回" forState:UIControlStateNormal];

        // [loginButton addTarget:self action:@selector(login:)

        //       forControlEvents:UIControlEventTouchUpInside];

        [_loginView addSubview:regetButton];

  • 相关阅读:
    linux进程间通信--信号量
    linux进程间通信--信号通信
    linux进程间通信--管道通信
    探究守护进程及其错误日志处理
    探究wait与waitpid之间的那些事
    探究一下strtok的用法
    文件IO与标准IO探究及总结
    Linux 库的制作--动态库与静态库
    python基础使用
    linux正则表达式使用
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4131879.html
Copyright © 2011-2022 走看看