首页 > 代码库 > 控件设置属性(UIButton,UITextField)

控件设置属性(UIButton,UITextField)

//延展
@interface AppDelegate ()
{
    UIView *_View;
}
//不是一开始定义类的时候定义的实例变量,而是后期根据需求而定义的实例变量,统一定义在.m文件中的延展中,外界不可见.
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIImage * ima = [UIImage imageNamed:@"a.jpg"];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    _View = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    _View.backgroundColor = [UIColor grayColor];
    [self.window addSubview:_View];
    [_View release];
    
    //创建UILabel
    [self createLabel];
    //创建UITextField
    [self createTextField];
    //创建UIAlertView
    [self createAlertView];
    //创建UIButton
    [self createButton];
    
    return YES;
}

- (void)createButton
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(30, 300, 100, 40);
    button.backgroundColor = [UIColor whiteColor];
    
    button.layer.cornerRadius = 5;
    
    NSInteger a = 1;
    [button setTitle:[NSString stringWithFormat: @"%ld",a] forState:UIControlStateNormal];
    
    //给button添加响应事件
    button.tag = 100 + a;

    //click 后面的参数为 : 谁调用addTarget:action: 方法 .参数就是谁. 而且参数只能是一个.
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    
    [_View addSubview:button];
}

- (void)click :(UIButton *)button
{
   // button.textInputContextIdentifier = @"aaa";
    NSLog(@"%@",button);
}

- (void)createTextField
{
    //UITextField 是UIControl的子类,UIControl又是UIView的子类,所以也是一个视图,只不过比UIView多了两个功能:(1)文字显示(2)文本编辑
    //UITextField的使用步骤和UIView一样
    //1.创建对象
    UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 220, 30)];
    //2.配置属性
    field.backgroundColor = [UIColor whiteColor];
    
    //(1)设置 边框样式
    
    /**
     UITextBorderStyleNone,
     UITextBorderStyleLine,   边框
     UITextBorderStyleBezel,
     UITextBorderStyleRoundedRect 圆角
     */
    field.borderStyle = UITextBorderStyleRoundedRect;
    //(2)设置输入框默显示(提示文字)的文字,但是不做为文本内容的一部分
    field.placeholder = @"请输入用户名";
    //(3)设置开始显示的文字
    field.text = @"string";
    //(4)设置文本颜色
    field.textColor = [UIColor redColor];
    //(5)对齐方式
    field.textAlignment = NSTextAlignmentCenter;
    //(6)文本字体
    field.font = [UIFont fontWithName:@"Thonburi-Bold" size:20];
    //(7)是否输入框是否可编辑
    field.enabled = YES;
    //(8)开始时清空输入框
    field.clearsOnBeginEditing = YES;
    //(9)是否文字以圆点格式显示 (设置密码模式)
    field.secureTextEntry = YES;
    //(10) 设置弹出键盘的样式
//    field.keyboardType = UIKeyboardTypeNumberPad;
    
    //(11)键盘右下角的显示的样式
    field.returnKeyType = UIReturnKeyGo;
    
    //(11)代理
    //代理使用步骤:
        //1.设置代理
    
    field.delegate = self;
        //2.服从协议
            //  UITextFieldDelegate
        //3.实现协议中的方法
            //(BOOL)textFieldShouldReturn:(UITextField *)textField
    //(12)自定义输入视图
    UIView * v1 = [[UIView alloc]initWithFrame:CGRectMake(200, 0, 568, 100)];
    v1.backgroundColor = [UIColor redColor];
   // field.inputView = v1;
    // (13)输入视图上方的辅助视图
    field.inputAccessoryView = v1;
    //3.添加到父视图
    [_View addSubview:field];
    //4.释放所有权
    [field release];
}

//当点击键盘的右下角return按钮时触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //回收键盘 取消第一响应者
    [textField resignFirstResponder];
    NSLog(@"click");
    return YES;
}
- (void)createAlertView
{
    
}




//当在一个方法中要访问另外一个方法中定义的局部变量时,就把该变量定义为实例变量.或者在多个方法中想访问一个变量,也要把该变量声明为实例变量
- (void)createLabel
{
    /**
     UILabel 是iOS开发中用来显示文字的控件,是UIView的子类.右移具有UIView的所有功能,只不过比UIView多了文字显示的功能.
     UILabel的使用过程和UIView类似:
     1.创建对象
     2.配置属性
     3.添加到父视图
     4.释放所有权
     不同的控件之间只是配置的属性的不同,也就是差异所在,所以学习一个新控件时,只要配置该控件独有的属性即可.
     */
    UILabel * v1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    v1.backgroundColor =[UIColor whiteColor];
    //设置label上显示的文字
    v1.text = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //label 文字位置
    //0 NSTextAlignmentLeft        左对齐
    //1 NSTextAlignmentCenter    居中
    //2 NSTextAlignmentRight      右对齐
    v1.textAlignment = NSTextAlignmentCenter;
    //文字换行  默认为1 如果不限制行数将值设置为 0
    v1.numberOfLines = 0;
    //换行的标准(文本的截取原则) 默认 以单词为一组
    /**
     NSLineBreakByWordWrapping = 0,          Wrap at word boundaries, default
     NSLineBreakByCharWrapping,         Wrap at character boundaries
     NSLineBreakByClipping,              Simply clip
     NSLineBreakByTruncatingHead,     Truncate at head of line: "...wxyz"
     NSLineBreakByTruncatingTail,     Truncate at tail of line: "abcd..."
     NSLineBreakByTruncatingMiddle     Truncate middle of line:  "ab...yz"
     */
    v1.lineBreakMode = NSLineBreakByClipping;
    
    //设置阴影的偏移量 正值向X Y 轴 正方向偏移 负值向反方向偏移.
    v1.shadowOffset = CGSizeMake(2, 2) ;
    //设置阴影的颜色
    v1.shadowColor = [UIColor yellowColor];
    // label 文字颜色
    v1.textColor = [UIColor redColor];
    //label 文字大小
    //(1)字体样式
    
    //(2)字号(默认 17)
    //systemFontOfSize: 默认使用系统默认字体,可以更改字体大小.
    v1.font = [UIFont systemFontOfSize:25];
    
    v1.font = [UIFont fontWithName:@"Thonburi-Bold" size:20];
    //    //UIFont familyNames 获取字体家族的名字
    //    NSLog(@"%@",[UIFont familyNames]);
    //    //UIFont fontNamesForFamilyName:@"Thonburi" 获取对应字体下属的字体名字
    //    NSLog(@"%@",[UIFont fontNamesForFamilyName:@"Thonburi"]);
    
    
    [_View addSubview:v1];
    [v1 release];
    
    
    

}

控件设置属性(UIButton,UITextField)