1.按钮(Button)
- - (void)addButton:(id)sender
- {
- //创建一个按钮
- UIButton *pBtton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- //设置区域
- [pBtton setFrame:CGRectMake(10, 70, 100, 40)];
- //设置按钮的标题和响应方式
- [pBtton setTitle:@"Normal" forState:UIControlStateNormal];
- //允许显示高亮
- pBtton.showsTouchWhenHighlighted = YES;
- //为按钮设置方法,需要写出方法的实现
- [pBtton addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
- //把方法加到当前的视图中
- [self.view addSubview:pBtton];
- }
2.标签栏(Label)
- - (void)addLabel:(id)sender
- {
- //创建一个label
- UILabel *pLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 50)];
- //内容
- pLabel.text = @"HelloWorld\nSecondLine";
- //设置字体和大小
- pLabel.font = [UIFont fontWithName:@"Verdana" size:18];
- //字体对齐方式
- pLabel.textAlignment = NSTextAlignmentCenter;
- //字体颜色
- pLabel.textColor = [UIColor redColor];
- //显示行数
- pLabel.numberOfLines = 2;
- //阴影颜色
- pLabel.shadowColor = [UIColor blackColor];
- //阴影尺寸
- pLabel.shadowOffset = CGSizeMake(2.0,1.0);
- //设置label的背景色为透明色
- pLabel.backgroundColor = [UIColor clearColor];
- //把标签添加到当前视图
- [self.view addSubview:pLabel];
- //把创建的变量释放
- [pLabel release];
- }
3.文本栏(textField)
- - (void)addTextField:(id)sender
- {
- //创建TextField
- UITextField *pTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 116, 200, 31)];
- //设置边框样式
- pTextField.borderStyle = UITextBorderStyleRoundedRect;
- //设置字体
- pTextField.font = [UIFont systemFontOfSize:18.0];
- //根据宽度改变字体
- pTextField.adjustsFontSizeToFitWidth = YES;
- //最小字体
- pTextField.minimumFontSize = 2.0;
- //清除按钮的样式
- pTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
- //弹出的键盘的样式
- pTextField.keyboardType = UIKeyboardTypeDefault;
- //设置使用自动更正功能
- pTextField.autocorrectionType = UITextAutocorrectionTypeNo;
- //设置键盘自动大小写的属性
- pTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
- //设置返回按钮类型
- pTextField.returnKeyType = UIReturnKeyDone;
- //设置是否支持密码文本显示
- pTextField.secureTextEntry = YES;
- //设置委托
- pTextField.delegate = self;
- //把文本栏添加到当前视图
- [self.view addSubview:pTextField];
- //把创建的变量释放
- [pTextField release];
- }
遵循UITextFieldDelegate协议,实现键盘的弹回方法
- - (BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- //放当前的textField放弃第一响应者
- [textField resignFirstResponder];
- return YES;
- }
遵循UITextFieldDelegate协议,实现控制文本栏字符串的输入长度控制
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {
- //设置最大可输入的字符长度
- int MAX_LENGTH = 10;
- NSMutableString *pNewString = [NSMutableString stringWithString:textField.text];
- //完成range内字符串的替换
- [pNewString replaceCharactersInRange:range withString:string];
- //根据两个的长度判断,返回YES or NO.
- return ([pNewString length] <= MAX_LENGTH);
- }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。