UI基础控件创建(UILabel、UITextField、UIButton)

UI基础控件创建(UILabel、UITextField、UIButton)

 

UILabel

    //UILabel;
    UILabel *nameLabel = [[UILabel alloc] init];//初始化
    nameLabel.frame = CGRectMake(10, 100, 100, 30);//位置,和大小
    nameLabel.text = @"Tiotoy";                     //Label所显示的内容
    nameLabel.text.font = [UIFont systemFontOfSize:18];    //设置字体
    nameLabel.textColor = [UIColor redColor];       //Label的字体颜色
    nameLabel.backgroundColor = [UIColor yellowColor];//Label的北京颜色
    nameLabel.textAlignment = UITextAlignmentCenter;    //Label中字体的对齐方式
    [self.view addSubview:nameLabel];
    [nameLabel release];

 

UITextField (需要设置代理)<UITextFieldDelegate>

    //UITextField (需要设置代理)<UITextFieldDelegate>
    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 140, 100, 30)];//初始化
    textFiled.delegate = self;              //设置代理
    textFiled.borderStyle = UITextBorderStyleRoundedRect;//UITextField的边框样式
    textFiled.keyboardType = UIKeyboardTypeDefault;//键盘的类型
    textFiled.returnKeyType = UIReturnKeyDone;      //返回键的类型
    textFiled.clearButtonMode = UITextFieldViewModeAlways;  //用于添加文字后面的清除按钮
    [self.view addSubview:textFiled];//把 UITextField对象 添加到一个View上
    [textFiled release];

 

UIButton

    //UIButton 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];//初始化
    [btn setFrame:CGRectMake(100,100,100, 30)];    //设置Frame
    [btn setTitle:@"123" forState:UIControlStateNormal];//设置按钮的Title,和状态,此为正常状态
    [btn setTitle:@"456" forState:UIControlStateHighlighted];//设置按钮的Title,和状态,此为高亮状态
    [btn setImage:[UIImage imageNamed:@"button_all.png"] forState:UIControlStateNormal];//将正常状态的显示成图片
    [btn setImage:[UIImage imageNamed:@"button_End.png"] forState:UIControlStateHighlighted];//将高亮状态显示为图片
    //点击Button时的触发事件
    [btn addTarget:self         //实现事件的位置
            action:@selector(doAction) //点击Button时的会触发此方法
  forControlEvents:UIControlEventTouchUpInside];//触发的方式
    [self.window addSubview:btn];
---------------------------------------------------------------------------------------------
扩充:
    UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
---------------------------------------------------------------------------------------------
    UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
    [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
---------------------------------------------------------------------------------------------

 

 

posted @ 2012-12-26 15:30  Tiotoy  阅读(257)  评论(0编辑  收藏  举报