【问题标题】:UITextField - Rounded corner issueUITextField - 圆角问题
【发布时间】:2012-11-22 21:52:24
【问题描述】:

我提出了我面临的问题。我正在以编程方式创建UITextField,如下所示。

UItextField *mobileNumberField = [[UITextField alloc] initWithFrame:CGRectMake(10, 195, 300, 41)];
mobileNumberField.delegate = self;
mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;
[mobileNumberField.layer setCornerRadius:14.0f];
mobileNumberField.placeholder = @"Mobile Number";
[self.paymentsHomeView addSubview:mobileNumberField];

输出是附加的图像。

我不知道为什么它会在拐角处折断。帮助我修复我的文本字段,如下图所示。

【问题讨论】:

    标签: iphone objective-c ios ios5 uitextfield


    【解决方案1】:

    只需删除此行...

    mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;
    

    并添加此代码..

        [mobileNumberField setBackgroundColor:[UIColor whiteColor]];
        [mobileNumberField.layer setBorderColor:[UIColor grayColor].CGColor];
        [mobileNumberField.layer setBorderWidth:1.0];
    

    【讨论】:

      【解决方案2】:

      以上所有答案都很好,但这里我是通过@IBDesignable 添加代码。

      @IBDesignable class DesignableUITextField: UITextField {
      
      // Provides left padding for images
      override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
          var textRect = super.leftViewRect(forBounds: bounds)
          textRect.origin.x += leftPadding
          return textRect
      }
      
      // Provides right padding for images
      override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
          var textRect = super.rightViewRect(forBounds: bounds)
          textRect.origin.x -= rightPadding
          return textRect
      }
      
      @IBInspectable var leftImage: UIImage? {
          didSet {
              updateView()
          }
      }
      
      @IBInspectable var rightImage: UIImage? {
          didSet {
              updateRightView()
          }
      }
      
      @IBInspectable var leftPadding: CGFloat = 0
      @IBInspectable var rightPadding: CGFloat = 0
      @IBInspectable var gapPadding: CGFloat = 0
      @IBInspectable var color: UIColor = UIColor.lightGray {
          didSet {
              updateView()
          }
      }
      @IBInspectable var cornerRadius: CGFloat = 0
      @IBInspectable var borderColor: UIColor? = .lightGray
      
      override func draw(_ rect: CGRect) {
          layer.cornerRadius = cornerRadius
          layer.masksToBounds = true
          layer.borderWidth = 1
          layer.borderColor = borderColor?.cgColor
          
      }    
      
      //@IBInspectable var roundCornersRadius: CGFloat = 0 {
        //  didSet{
           //   roundCornersRadiusTextField(radius: roundCornersRadius)
        //  }
      // }
      
      
      func roundCornersRadiusTextField(radius:CGFloat) {
             roundCorners(corners: [UIRectCorner.topLeft, UIRectCorner.topRight, UIRectCorner.bottomLeft, UIRectCorner.bottomRight], radius:radius)
         }
      
         func roundBottomCornersRadius(radius:CGFloat) {
             roundCorners(corners: [UIRectCorner.topLeft, UIRectCorner.topRight], radius:radius)
         }
      
      func updateView() {
          if let image = leftImage {
              leftViewMode = UITextField.ViewMode.always
              let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
              imageView.contentMode = .scaleAspectFit
              imageView.image = image
              // Note: In order for your image to use the tint color, you have to select the image in the Assets.xcassets and change the "Render As" property to "Template Image".
              imageView.tintColor = color
              leftView = imageView
          } else {
              leftViewMode = UITextField.ViewMode.never
              leftView = nil
          }
          
          // Placeholder text color
          attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: color])
      }
      
      
      func updateRightView() {
          if let image = rightImage {
              rightViewMode = UITextField.ViewMode.always
              let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
              imageView.contentMode = .scaleAspectFit
              imageView.image = image
              // Note: In order for your image to use the tint color, you have to select the image in the Assets.xcassets and change the "Render As" property to "Template Image".
              imageView.tintColor = color
              rightView = imageView
          } else {
              rightViewMode = UITextField.ViewMode.never
              rightView = nil
          }
          
          // Placeholder text color
          attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSAttributedString.Key.foregroundColor: color])
      }
      
      func roundCorners(corners:UIRectCorner, radius:CGFloat) {
              let bounds = self.bounds
              
              let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
              
              let maskLayer = CAShapeLayer()
              maskLayer.frame = bounds
              maskLayer.path = maskPath.cgPath
              
              self.layer.mask = maskLayer
              
              let frameLayer = CAShapeLayer()
              frameLayer.frame = bounds
              frameLayer.path = maskPath.cgPath
              frameLayer.strokeColor = UIColor.darkGray.cgColor
              frameLayer.fillColor = UIColor.init(red: 247, green: 247, blue: 247, alpha: 0).cgColor
              
              self.layer.addSublayer(frameLayer)
          }
      
      private var textPadding: UIEdgeInsets {
          let p: CGFloat = leftPadding + gapPadding + (leftView?.frame.width ?? 0)
          return UIEdgeInsets(top: 0, left: p, bottom: 0, right: 5)
      }
      override open func textRect(forBounds bounds: CGRect) -> CGRect {
          return bounds.inset(by: textPadding)
      }
      
      override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
          return bounds.inset(by: textPadding)
      }
      
      override open func editingRect(forBounds bounds: CGRect) -> CGRect {
          return bounds.inset(by: textPadding)
      }}
      

      【讨论】:

        【解决方案3】:

        以下代码在 Swift 5、XCode 11.4 中为我提供了以下结果。
        绝对可以添加更多的花里胡哨。我觉得这个MVP不错

            naviTextField = UITextField.init(frame: CGRect(x: 0, y: 0, width: (self.navigationController?.navigationBar.frame.size.width)!, height: 21))
            self.navigationItem.titleView = naviTextField
            naviTextField.becomeFirstResponder()
            naviTextField.placeholder = "Type target name here"
            naviTextField.borderStyle = .roundedRect
            naviTextField.layer.cornerRadius = 5.0
            naviTextField.textAlignment = .center
        

        【讨论】:

          【解决方案4】:

          快速解决方案:

          textField.layer.borderWidth = anyWidth
          textField.layer.borderColor = anyColor
          textField.layer.cornerRadius = textField.frame.size.height/2
          textField.clipsToBounds = true
          

          【讨论】:

            【解决方案5】:
            textField.layer.cornerRadius=textfield.frame.size.height/2;
            
            textField.clipsToBounds=YES;
            

            【讨论】:

            • 它创建循环文本字段
            【解决方案6】:

            Swift 3 解决方案:

            我已经编写了单独的函数来快速设置任何图层的边框和角半径,您只需将任何视图的图层、边框宽度、角半径和边框颜色传递给以下函数

            ` func setBorderAndCornerRadius(layer: CALayer, width: CGFloat, radius: CGFloat,color : UIColor ) {
                    layer.borderColor = color.cgColor
                    layer.borderWidth = width
                    layer.cornerRadius = radius
                    layer.masksToBounds = true
                }
            

            `

            【讨论】:

              【解决方案7】:

              切角的原因是文本字段有一个封闭的视图。当您设置角半径时,适用于该视图,因此内部文本字段的角似乎被切割 - 实际上它们甚至没有改变。

              解决方案是将UITextField 放在UIView 中,将文本字段的边框样式设置为无。然后将边框和角半径规范应用到 uiview。注意边框颜色,它与 UITextField 边框颜色非常接近,如果不相同的话。

              在 Xcode 7.3.1、Swift 2.2、iOS 8 和 9 中编写、测试和工作。

              斯威夫特:

              textField.borderStyle = UITextBorderStyle.None
              textBorderView.layer.cornerRadius = 5
              textBorderView.layer.borderWidth = 1
              textBorderView.layer.borderColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.2).CGColor
              

              【讨论】:

                【解决方案8】:

                这是您问题的解决方案

                UITextField * txtField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
                
                [txtField setBorderStyle:UITextBorderStyleNone];
                
                [txtField.layer setMasksToBounds:YES];
                [txtField.layer setCornerRadius:10.0f];
                [txtField.layer setBorderColor:[[UIColor lightGrayColor]CGColor]];
                [txtField.layer setBorderWidth:1];
                [txtField setTextAlignment:UITextAlignmentCenter];
                [txtField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
                [self.view addSubview:txtField];
                

                【讨论】:

                • 它正在工作,但是当我应用它时,占位符文本和 txtField 的前导边框之间没有位置。你知道如何解决这个问题(我尝试在占位符的开头放置空白:“placeholderText”而不是“placeholderText”但不起作用)?
                【解决方案9】:

                在下面更新你的喜欢。

                    UITextField *mobileNumberField = [[UITextField alloc] initWithFrame:CGRectMake(10, 195, 300, 41)];
                    mobileNumberField.delegate = self;
                    mobileNumberField.layer.borderWidth = 1.0f;
                    mobileNumberField.layer.borderColor = [UIColor lightGrayColor].CGColor;
                    mobileNumberField.
                //    mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;
                    [mobileNumberField.layer setCornerRadius:14.0f];
                    mobileNumberField.placeholder = @"Mobile Number";
                    [self.paymentsHomeView addSubview:mobileNumberField];
                

                【讨论】:

                • 文本字段默认采用 UITextBorderStyleNone。如果我删除上面的行,就没有边框了。
                • 你可以设置borderColor。
                • 尝试设置边框颜色。但它不适用,因为没有边界。
                • 谢谢你..!从 Joshi 的回答中找到了解决方案。
                猜你喜欢
                • 2015-08-23
                • 1970-01-01
                • 2011-01-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-16
                相关资源
                最近更新 更多