【问题标题】:TextField Corner Radius : Not Sharp Corners文本字段角半径:不是尖角
【发布时间】:2017-03-12 00:06:26
【问题描述】:

我正在尝试在 uitextfield 效果上添加角半径,但不是锐角。

请查看附件图片。

我期待这样的结果。

这是我的灰色文本字段代码。

 self.layer.cornerRadius = 7.5
 self.layer.borderWidth = 1.5
 self.layer.borderColor =  UIColor(red: 209/255.0, green: 209/255.0, blue: 209/255.0, alpha: 1.0).CGColor 
 self.backgroundColor = UIColor.whiteColor() 

这是我的红色文本字段代码。

 self.layer.cornerRadius = 7.5
 self.layer.borderWidth = 1.5
 self.layer.borderColor = TextFieldRedBorderColor.CGColor
 self.backgroundColor = TextFieldRedBackgroundColor 

两个代码之间只有颜色差异,但仍然无法正常工作。

【问题讨论】:

    标签: ios swift uitextfield uikit calayer


    【解决方案1】:
    self.layer.masksToBounds = true
    

    这将解决您的问题

    【讨论】:

      【解决方案2】:
       @IBOutlet var passwordboarderview: UIView!
       @IBOutlet var textusername: UITextField!
       @IBOutlet var txtpassword: UITextField!
      
       passwordboarderview.layer.cornerRadius = 7.5
       passwordboarderview.layer.borderColor = UIColor.whiteColor().CGColor
       passwordboarderview.layer.borderWidth = 2.0
      
       passwordboarderview.clipsToBounds = true
       txtpassword.attributedPlaceholder = NSAttributedString(string:"Password",attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])   
      
       let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
       textusername.leftView = paddingView
       textusername.leftViewMode = .Always
       textusername.layer.borderWidth = 2.0
       textusername.layer.cornerRadius = 7.5
       textusername.layer.borderColor = UIColor.grayColor().CGColor
       textusername.clipsToBounds = true
      

      输出

      故事板

      【讨论】:

        【解决方案3】:
        //
        //  FDTextField.swift
        //  FilloutDocs
        //
        //  Created by Janak Thakkar on 15/03/16.
        //  Copyright © 2016 zetrixweb. All rights reserved.
        //
        
        import UIKit
        
        private var maxLengthDictionary = [UITextField : Int]()
        
        @IBDesignable
        
        class ZWTextField : UITextField {
        
            var topBorder: UIView?
            var bottomBorder: UIView?
            var leftBorder: UIView?
            var rightBorder: UIView?
            var leftimageview : UIImageView?
            var rightimageview : UIImageView?
            @IBInspectable var borderColor: UIColor = UIColor.clearColor() {
                didSet {
                    layer.borderColor = borderColor.CGColor
                }
            }
        
            @IBInspectable var ULText: Bool = false {
                didSet {
                    let textRange = NSMakeRange(0, (self.text?.characters.count)!)
                    let attributedText = NSMutableAttributedString(string: (self.text)!)
                    attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
                    // Add other attributes if needed
        
                    self.attributedText = attributedText
                }
            }
            @IBInspectable var borderWidth: CGFloat = 0 {
                didSet {
                    layer.borderWidth = borderWidth
                }
            }
        
            @IBInspectable var cornerRadius: CGFloat = 0 {
                didSet {
                    layer.cornerRadius = cornerRadius
                }
            }
        
            @IBInspectable var fitToWidth: Bool = false {
                didSet {
                    adjustsFontSizeToFitWidth = fitToWidth
                }
            }
            @IBInspectable var cursorColor : UIColor = UIColor.blueColor(){
                didSet {
                    tintColor = cursorColor
                }
            }
        
            @IBInspectable var placeHolderColor : UIColor = UIColor.lightGrayColor(){
                didSet {
                    setValue(placeHolderColor, forKeyPath: "_placeholderLabel.textColor")
                }
            }
            override func layoutSubviews() {
                super.layoutSubviews()
            }
            @IBInspectable var rightImage : UIImage? {
                didSet {
                    if rightImage != nil {
                        let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth :  rightImage!.size.width + 10
                        rightViewMode = UITextFieldViewMode.Always
                        rightimageview = UIImageView()
                        rightimageview!.frame=CGRectMake(self.frame.size.width - width, self.frame.origin.y+2, width,self.frame.size.height-4)
                        rightimageview!.image = rightImage
                        rightView = rightimageview
                        self.rightViewMode = .Always
                        rightimageview!.contentMode = .Center
                    }
                    else {
                        if rightimageview != nil {
                            rightimageview?.removeFromSuperview()
                            rightimageview = nil
                        }
                    }
                }
            }
            @IBInspectable var rightviewWidth : CGFloat = 0 {
                didSet{
                    if rightimageview != nil{
                        let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth :  rightImage!.size.width + 10
                        rightimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
                    }
                }
            }
            @IBInspectable var leftImage : UIImage? {
                didSet {
                    if leftImage != nil {
                        let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth :  leftImage!.size.width + 10
                        leftViewMode = UITextFieldViewMode.Always
                        leftimageview = UIImageView();
                        leftimageview!.frame=CGRectMake(self.frame.origin.x+50, self.frame.origin.y+5, width,self.frame.size.height-5)
                        //leftimageview!.frame = CGRectMake(10, 5, 40, 40)
                        leftimageview!.image = leftImage;
                        leftView = leftimageview;
                        self.leftViewMode = .Always
                        leftimageview!.contentMode = .Center
                    }
                }
            }
            @IBInspectable var leftviewWidth : CGFloat = 0 {
                didSet{
                    if leftimageview != nil{
                        let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth :  leftImage!.size.width + 10
                        leftimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
                    }
                }
            }
            @IBInspectable var bottomLineWidth : CGFloat = 1 {
                didSet{
                    let border: CALayer = CALayer()
                    border.borderColor = UIColor.darkGrayColor().CGColor
                    self.frame = CGRectMake(0, self.frame.size.height - bottomLineWidth, self.frame.size.width, self.frame.size.height)
                    border.borderWidth = borderWidth
                    self.layer.addSublayer(border)
                    self.layer.masksToBounds = true
                }
            }
        
            @IBInspectable var bottomLineColor : UIColor = UIColor.lightGrayColor(){
                didSet {
                    let border: CALayer = CALayer()
                    border.borderColor = bottomLineColor.CGColor
                }
            }
            @IBInspectable var paddingLeft: CGFloat = 0
            @IBInspectable var paddingRight: CGFloat = 0
        
            override func textRectForBounds(bounds: CGRect) -> CGRect {
                return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y,
                    bounds.size.width - paddingLeft - paddingRight, bounds.size.height);
            }
        
            override func editingRectForBounds(bounds: CGRect) -> CGRect {
                return textRectForBounds(bounds)
            }
            @IBInspectable var topBorderColor : UIColor = UIColor.clearColor()
            @IBInspectable var topBorderHeight : CGFloat = 0 {
                didSet{
                    if topBorder == nil{
                        topBorder = UIView()
                        topBorder?.backgroundColor=topBorderColor;
                        topBorder?.frame = CGRectMake(0, 0, self.frame.size.width, topBorderHeight)
                        addSubview(topBorder!)
                    }
                }
            }
            @IBInspectable var bottomBorderColor : UIColor = UIColor.clearColor()
            @IBInspectable var bottomBorderHeight : CGFloat = 0 {
                didSet{
                    if bottomBorder == nil{
                        bottomBorder = UIView()
                        bottomBorder?.backgroundColor=bottomBorderColor;
                        bottomBorder?.frame = CGRectMake(0, self.frame.size.height - bottomBorderHeight, self.frame.size.width, bottomBorderHeight)
                        addSubview(bottomBorder!)
                    }
                }
            }
            @IBInspectable var leftBorderColor : UIColor = UIColor.clearColor()
            @IBInspectable var leftBorderHeight : CGFloat = 0 {
                didSet{
                    if leftBorder == nil{
                        leftBorder = UIView()
                        leftBorder?.backgroundColor=leftBorderColor;
                        leftBorder?.frame = CGRectMake(0, 0, leftBorderHeight, self.frame.size.height)
                        addSubview(leftBorder!)
                    }
                }
            }
            @IBInspectable var rightBorderColor : UIColor = UIColor.clearColor()
            @IBInspectable var rightBorderHeight : CGFloat = 0 {
                didSet{
                    if rightBorder == nil{
                        rightBorder = UIView()
                        rightBorder?.backgroundColor=topBorderColor;
                        rightBorder?.frame = CGRectMake(self.frame.size.width - rightBorderHeight, 0, rightBorderHeight, self.frame.size.height)
                        addSubview(rightBorder!)
                    }
                }
            }
            @IBInspectable var maxLength: Int {
                get {
                    if let length = maxLengthDictionary[self] {
                        return length
                    } else {
                        return Int.max
                    }
                }
                set {
                    maxLengthDictionary[self] = newValue
                    addTarget(self, action: "checkMaxLength:", forControlEvents: UIControlEvents.EditingChanged)
                }
            }
        
            func checkMaxLength(sender: UITextField) {
                let newText = sender.text
                if newText?.characters.count > maxLength {
                    let cursorPosition = selectedTextRange
                    text = (newText! as NSString).substringWithRange(NSRange(location: 0, length: maxLength))
                    selectedTextRange = cursorPosition
                }
            }
        
        
        }
        

        只要在项目中添加这个文件,你就可以设置所有的属性,并且可以实时看到。所有的属性你可以通过storyboard设置。

        【讨论】:

        • 虽然它可能是一个有趣的代码,但它在上面添加了太多内容,以至于问题的答案不可见。
        • 我尝试使用 @IBInspectable 覆盖所有属性,如果您还需要更多属性,请告诉我。
        • 这里我已经把示例代码上传到github ::: github.com/ZetrixWeb-iOS/TextField---IBDesignable
        【解决方案4】:

        我的代码中只有一个错误。

        TextField 边框样式是线条

        textField.borderStyle = .Line
        

        然后我把它设为无

        textField.borderStyle = .None
        

        它就像我想要的那样工作(圆角)......

        无论如何..感谢您的支持和回答...

        【讨论】:

          猜你喜欢
          • 2018-09-07
          • 2018-08-12
          • 1970-01-01
          • 1970-01-01
          • 2015-06-03
          • 1970-01-01
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多