- (void)viewDidLoad { [super viewDidLoad]; lb=[[UILabel alloc] initWithFrame:CGRectMake(60, 150, 200, 50)]; [email protected]"label"; ///字体颜色 lb.textColor =[UIColor blueColor]; ///背景颜色 lb.backgroundColor=[UIColor grayColor]; ///对齐方式 lb.textAlignment=NSTextAlignmentCenter; [self.view addSubview:lb]; // UIImage *image = [UIImage imageNamed:@"001.png"]; // [self.view addSubview:image]; textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 70)]; ///背景颜色 //[textfield setBackgroundColor:[UIColor redColor]]; textfield.backgroundColor=[UIColor grayColor]; ///对齐方式 textfield.textAlignment=NSTextAlignmentCenter; ///边框样式 textfield.borderStyle=UITextBorderStyleBezel; ///占位符 [email protected]"请输入账号"; ///密码输入 textfield.secureTextEntry=YES; ///垂直模式 textfield.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; ///清除按钮 textfield.clearButtonMode=UITextFieldViewModeAlways; ///返回键类型 textfield.returnKeyType=UIReturnKeyDone; ///键盘类型 textfield.keyboardType=UIKeyboardTypeAlphabet; ///设置代理 textfield.delegate=self; [self.view addSubview:textfield]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } ///每一次改变输入框的内容都会掉用 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSLog(@"range:%@,string \"%@\"",NSStringFromRange(range),string); ///禁止输入的内容 if ([string isEqualToString:@"h"]) { return NO; } return YES; } ///触发点击事件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [textfield resignFirstResponder]; lb.text=textfield.text; } ///return触发 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textfield resignFirstResponder]; lb.text=textfield.text; return YES; } ///允许/禁止清除按钮 - (BOOL)textFieldShouldClear:(UITextField *)textField{ return YES; } @end
时间: 2024-10-04 14:12:25