【问题标题】:rounded corner to UITextField XcodeUITextField Xcode的圆角
【发布时间】:2015-08-23 20:40:48
【问题描述】:

我有两个文本字段(用户名和密码),并希望根据附件制作用户名的顶部圆角和密码的底部圆角。

【问题讨论】:

标签: ios swift xcode user-interface user-experience


【解决方案1】:

只需创建一个UIView

将您的两个文本字段放在UIView 中。 去掉UITextField的边框样式。

yourView.layer.cornerRadius = 10.0
yourView.clipsToBounds = true

【讨论】:

    【解决方案2】:

    你可以使用:

    (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
    

    例子:

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path  = maskPath.CGPath;
    textField.layer.mask = maskLayer;
    

    【讨论】:

      【解决方案3】:

      只需按照以下步骤操作

      第 1 步:在您的班级中导入 QuartzCore 框架:

            #import <QuartzCore/QuartzCore.h>
      

      第 2 步:应用编码

            textField.layer.cornerRadius=6.0f;
            textField.layer.masksToBounds=YES;
            textField.layer.borderColor=[[UIColor blueColor]CGColor];
            textField.layer.borderWidth= 1.0f; 
      
               or
      
            [textField.layer setBorderColor:[[UIColor blueColor] CGColor]];
            [textField.layer setBorderWidth:2.o];
            [textField.layer setCornerRadius:5.0]; 
      

      【讨论】:

        【解决方案4】:

        我认为你应该编写 CALayer 扩展。 Swift 3.x - 4.x

        extension CALayer {
        
            func addRadius(_ corners: UIRectCorner, radius: CGFloat, view: UIView) {
                let mask = CAShapeLayer()
                mask.bounds = view.frame
                mask.position = view.center
                mask.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
                view.layer.mask = mask
            }
        
            func addRadius(radius: CGFloat) {
                self.cornerRadius = radius
            }  
        }
        

        使用

            class ViewController: UIViewController {
        
                @IBOutlet var messageTextField: UITextField!
        
                override func viewDidLoad() {
                    super.viewDidLoad()
                    messageTextField.layer.addRadius(radius: 8.0)
                }
            }
        

        希望您的帮助:)

        【讨论】:

          【解决方案5】:

          这是一个完整的代码示例:

          导入 UIKit

          class RoundedTextField: UITextField {
          
              override func awakeFromNib() {
                  self.layer.cornerRadius = 10.0 //self.frame.size.height/2
                  self.clipsToBounds = false
          
                  super.awakeFromNib()
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-11-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-27
            • 2011-07-24
            • 2012-04-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多