Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Providing Extra padding in UITextField

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 904
    Comment on it

    UITextField is an editable area where one can use to get input from user using keyboard.


    We often come to a situation where extra padding is required from the left or right depending on requirement. Padding is already provided by UITextField for textfield of border styles “RoundedRect” and “Bezel” but for other styles it’s not.


    To have extra padding subclass a UITextField and override “textRectForBounds” and “editingRectForBounds”.


    Below is the code snippet adding extra padding from left, you can add padding from left as well..
     

    Code Snippet

    class MYUITextField: UITextField {
        
        
        required init?(coder aDecoder: NSCoder){
            super.init(coder: aDecoder)
        }
        
        override func textRectForBounds(bounds: CGRect) -> CGRect {
            
            return CGRectMake(10, bounds.origin.y, bounds.size.width-10, bounds.size.height); // here 10 is padding or spacing from left
        }
        
        override func editingRectForBounds(bounds: CGRect) -> CGRect {
            return self.textRectForBounds(bounds);
        }
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: