iOS的一些基本控件的使用

1、UITextField的使用

   //初始化

    self.txtName=[[UITextField alloc] initWithFrame:CGRectMake(100, 20, 200, 50)];

    //添加背景颜色

    self.txtName.backgroundColor=[UIColor redColor];

    //文本框的边框线型

    self.txtName.borderStyle=UITextBorderStyleLine;

    //编辑文本框的提示,在键盘中输入文字时,“请输入姓名”会自动消失

    self.txtName.placeholder=@"请输入姓名";

    //把txtName添加到view上

    [self.view addSubview:self.txtName];

    //手动编辑文本框

    self.txtName.text=@"123456";

    //设置文本框字体的颜色

    self.txtName.textColor=[UIColor purpleColor];

     //设置整体字体背景颜色

    NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:@"lamco" attributes:@{NSBackgroundColorAttributeName:[UIColor redColor]}];

    

    [str addAttributes:@{NSBackgroundColorAttributeName:[UIColor grayColor]}range:NSMakeRange(2, 3)] ;

    [str addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, 3)];

    self.txtName.attributedText=str;

    

    self.txtName.returnKeyType=UIReturnKeySearch;

    self.txtName.keyboardType=UIKeyboardTypePhonePad;

2.键盘的隐藏于弹出方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if ([textField isFirstResponder]) {

        [textField resignFirstResponder];

    }

    return YES;

}

3.UISegmentedControl的使用

//设置Segment的显示项

    self.mySegment=[[UISegmentedControl alloc] initWithItems:@[@"red",@"green",@"blue"]];

    //设置默认选中项

    self.mySegment.selectedSegmentIndex=1;

    //添加项

    [self.mySegment insertSegmentWithTitle:@"yellow" atIndex:3 animated:YES];

    //设置位置

    self.mySegment.frame=CGRectMake(100, 100, 200, 30);

    //把mySegment添加到父视图view中

    [self.view addSubview:self.mySegment];

   4.UIStepper的使用    

         self.myStepper=[[UIStepper alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];

    //设置最小值

    self.myStepper.minimumValue=1.0;

    //设置最大值

    self.myStepper.maximumValue=10.0;

    //设置每次所加的数

    self.myStepper.stepValue=1.0;

    //前景色

    self.myStepper.tintColor=[UIColor redColor];

    //设置循环

    self.myStepper.wraps=YES;

    [self.view addSubview:self.myStepper];

原文地址:https://www.cnblogs.com/zhaochaobin/p/5259981.html