🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ #import "ViewController.h" @interface ViewController () @end @implementation ViewController UITextField *textFiled; - (void)viewDidLoad { [super viewDidLoad]; [self addTextField]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) addTextField { UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero]; prefixLabel.text=@"输入用户名:"; [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]]; [prefixLabel sizeToFit]; textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 280, 30)]; textFiled.borderStyle = UITextBorderStyleRoundedRect; textFiled.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; [textFiled setFont:[UIFont boldSystemFontOfSize:12]]; textFiled.placeholder = @"Simple Text filed"; [textFiled setTextColor:[UIColor blueColor]]; textFiled.leftView = prefixLabel; textFiled.leftViewMode = UITextFieldViewModeAlways; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setTitle:@"提交" forState:UIControlStateNormal]; [btn sizeToFit]; textFiled.rightView=btn; textFiled.rightViewMode = UITextFieldViewModeAlways; [btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside] ; [self.view addSubview:textFiled]; } -(void)onClick { NSLog(@"获取输入框值:%@",textFiled.text); } @end ~~~