【问题标题】:how to set border style of a UITextfield如何设置 UITextfield 的边框样式
【发布时间】:2011-08-12 23:41:09
【问题描述】:

如何以编程方式设置UITextField 的边框样式?

我正在像这样创建我的文本字段:

UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)];
tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];       
tfText.textAlignment = UITextAlignmentCenter;
[self.view addSubview:tfText];
[tfText release];

【问题讨论】:

    标签: iphone ios objective-c uitextfield


    【解决方案1】:

    您可以使用 Quartzcore 和图层属性。

    sometextfield.layer.borderWidth = 1;
    sometextfield.layer.borderColor = [[UIColor redColor] CGColor];
    

    我必须记得将 QuartzCore 添加到您的项目中,并将其导入您要使用的地方。

    【讨论】:

      【解决方案2】:

      试试这个

      UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)];
      tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];       
      tfText.textAlignment = UITextAlignmentCenter;
      // Border Style None
      [tfText setBorderStyle:UITextBorderStyleNone];
      [self.view addSubview:tfText];
      [tfText release];
      

      供参考

      【讨论】:

        【解决方案3】:
        [pTxtTithiTime.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
        
        [pTxtTithiTime.layer setBorderWidth:0.8];
        

        //圆角部分,您指定视图的圆角半径:

          pTxtTithiTime.layer.cornerRadius = 5;
        
          pTxtTithiTime.clipsToBounds = YES;
        

        【讨论】:

          【解决方案4】:

          您可以像这样以编程方式制作 UITextField:

          UITextField *txtField = [[UITextField alloc]initWithFrame:CGRectMake(10, 260, 280, 30)]; // your required coordinate
          txtField.delegate =        self;
          txtField.placeholder =     @"My TextField";
          txtField.borderStyle =      UITextBorderStyleNone;
          txtField.keyboardType =        UIKeyboardTypeDefault;
          txtField.backgroundColor =       [self setBackGroundColorOnButton];
          txtField.layer.cornerRadius = 5.0;
          

          【讨论】:

            【解决方案5】:

            从以下四个开始,任何人都可以通过编程方式使用,

            [textField setBorderStyle:UITextBorderStyleNone];
            
            [textField setBorderStyle:UITextBorderStyleLine];
            
            [textField setBorderStyle:UITextBorderStyleBezel];
            
            [textField setBorderStyle:UITextBorderStyleRoundedRect];
            

            其中 textFieldUITextField

            的一个出口

            【讨论】:

              【解决方案6】:

              我知道,问题是关于 Obj-C,但我来这里是为了 Swift。在 Swift 中,它是这样完成的:

              txt.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0);
              txt.borderStyle = UITextBorderStyle.RoundedRect
              let myColor : UIColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0);
              txt.layer.borderWidth = 3;
              txt.layer.borderColor = myColor.CGColor;
              

              这会设置自定义背景颜色和相同颜色的边框(隐藏边框但在文本和文本框边框之间仍有一些填充。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-11-15
                • 2020-10-03
                • 1970-01-01
                相关资源
                最近更新 更多