美文网首页
UITextField、UILabel、UIButton的基本使

UITextField、UILabel、UIButton的基本使

作者: ThEAll | 来源:发表于2015-12-03 16:48 被阅读232次

    import "AppDelegate.h"

    @interface AppDelegate ()

    @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.
      self.window.backgroundColor = [UIColor whiteColor];
      [self.window makeKeyAndVisible];
      [self.window setRootViewController:[[UIViewController alloc]init]];

    // textField(输入框)的使用
    // 初始化并设置frame
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];
    // 设置占位字符(提示性文字)
    textField.placeholder = @"请输入手机号/邮箱";
    // 设置边框样式
    textField.borderStyle = UITextBorderStyleLine;
    // [textField setBackgroundColor : [UIColor redColor]];
    [self.window addSubview:textField];
    // 给字体设置颜色
    [textField setTextColor:[UIColor blackColor]];
    // 改变字体大小
    textField.font = [UIFont systemFontOfSize:15];
    // 得到系统支持的所有字体名称
    // NSLog(@"%@",[UIFont familyNames]);
    // 设置字体样式、大小
    textField.font = [UIFont fontWithName:@"Hiragino Sans" size:15];
    // 设置键盘样式
    [textField setKeyboardType:UIKeyboardTypeASCIICapable];
    // 设置键盘的外观
    [textField setKeyboardAppearance:UIKeyboardAppearanceDark];
    // 自动纠错设置
    [textField setAutocorrectionType:UITextAutocorrectionTypeYes];
    // 设置首字母大写 (首字母不能大写时,有可能和键盘样式有关)
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    // 一键清除(右边的小叉号)
    textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    // 当变为编辑状态时,让该textfeild刚刚所有的输入清除
    textField.clearsOnBeginEditing = YES;

    UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 260, 200, 50)];
    textField1.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:textField1];
    
    
    //  打开密码格式(密码样式是否可自定义)
    textField.secureTextEntry = YES;
    //  设置背景图像(如果不是PNG格式的,必须加后缀名,因为没有后缀名的时候,系统默认是PNG格式的)
    textField.background = [UIImage imageNamed:@"11.png"];
    //  文字的居左,居右,居中
    textField.textAlignment = NSTextAlignmentCenter;
    //  设置文字的垂直位置
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop ;
    //  不让进行编辑(交互)
    textField.enabled = NO; // enable:改变控件本身的状态,例如:textfield有可编辑状态和不可编辑状态。当设置为NO时,textfield就会从可编辑状态改变为不可编辑状态
    textField.userInteractionEnabled = NO; //  userInteractionEnabled:不可改变控件的状态,只是把用户交互关闭了,就是用户不可对该控件进行操作。
    

    // 添加辅助视图
    UIView *accessoryView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 50)];
    accessoryView.backgroundColor = [UIColor blackColor];
    textField.inputAccessoryView = accessoryView;

    //  自定义输入视图(自定义键盘)
    UIView *inputView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];
    textField.inputView =inputView1;
    
    //  左视图
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 100)];
    lable.backgroundColor = [UIColor redColor];
    textField.leftView = lable;
    lable.text = @"用户名";
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    //  右视图
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 60, 100)];
    lable1.backgroundColor = [UIColor greenColor];
    lable1.text = @"@sina.com";
    textField.rightView = lable1;
    textField.rightViewMode = UITextFieldViewModeAlways;
    

    // UIButton的使用
    // 按钮的初始化
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(150, 100, 100, 50);
    button.backgroundColor = [UIColor blackColor];
    // 设置按钮标题
    // normal:是按钮常态,不做任何操作和设置情况下看到的按钮状态
    // Highlighted: 用户按住按钮不松手时看到的状态
    [button setTitle:@"BMW" forState:UIControlStateNormal];
    [button setTitle:@"MJ" forState:UIControlStateHighlighted];
    // 关闭交互的状态标题(前一次操作后执行需要的时间较长,暂时关闭交互状态)
    // [button setTitle:@"JMN" forState:UIControlStateDisabled];
    // button.enabled = NO;
    // 按钮被选中状态
    // [button setTitle:@"一直被选中" forState:UIControlStateSelected];
    // 设置按钮被选中
    // [button setSelected:YES];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    // 当点击时有光圈效果
    button.showsTouchWhenHighlighted = YES;

    //  给按钮添加点击事件(目标动作机制)
    //  target:回调方法的执行者
    //  action:按钮的回调方法(点击所触发的事件),如果回调方法带参数,这个参数只能是按钮本身
    //  controlEvents:触发事件的点击模式
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:
     UIControlEventTouchUpInside];
    
    /*
    UIControlEventTouchDown  点击后触发事件
    UIControlEventTouchDownRepeat  双击后触发事件
    UIControlEventTouchDragInside  区域内拖拽触发事件
    UIControlEventTouchDragOutside  区域内拖拽至区域外,拖动过程中不要松手触发事件
    UIControlEventTouchDragEnter  从区域内拖拽出,不松手,在拖拽进去触发事件
    UIControlEventTouchDragExit  拖拽出去、进来、再出去时触发事件
    UIControlEventTouchUpInside  点击后触发事件(最常用)
    UIControlEventTouchUpOutside
    UIControlEventTouchCancel  多点触控时触发事件
    */
    
    //  关闭多点触控
    button.multipleTouchEnabled = NO;
    
    textField.tag = 1000; // 给textField添加tag值,tag的作用是,为当前view或者其子类添加标记,当前view或者其子类的父视图可以通过tag找到该视图。
    
    [self.window addSubview:button];
    return YES;
    

    }

    // button的点击事件实现部分
    -(void)buttonAction:(id)sender{
    UIButton button = (UIButton)sender;
    [button setTitle:@"hello world" forState:UIControlStateNormal];
    self.window.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    NSLog(@"@@@@@");

    //  点击按钮可以得到文本输入框中的文字
    //  textField不能是全局变量或者属性
    
    //  父视图通过tag值得到field
    
    UITextField *tagTextField = (UITextField *)[self.window viewWithTag:1000];
    //  得到文本框输入的内容
    NSString *textFieldStr = tagTextField.placeholder;
    NSLog(@"%@",textFieldStr);
    

    }

    相关文章

      网友评论

          本文标题:UITextField、UILabel、UIButton的基本使

          本文链接:https://www.haomeiwen.com/subject/wurshttx.html