Add lefthand margin to UITextField

I want to put the left margin of the UITextField text at 10 pixels. What is the best way to do this?

+23
alignment ios cocoa-touch uitextfield leftalign
Apr 15 '11 at 9:13
source share
9 answers

As I explained in a previous comment, the best solution in this case is to extend the UITextField class instead of using a category, so you can use it explicitly in the desired text fields.

 #import <UIKit/UIKit.h> @interface MYTextField : UITextField @end @implementation MYTextField - (CGRect)textRectForBounds:(CGRect)bounds { int margin = 10; CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height); return inset; } - (CGRect)editingRectForBounds:(CGRect)bounds { int margin = 10; CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height); return inset; } @end 

A category is intended to add new functions to an existing class, and not to override an existing method.

+19
Mar 28 2018-12-12T00:
source share

You can do this by extending the UITextField class and overriding two methods:

 - (CGRect)textRectForBounds:(CGRect)bounds; - (CGRect)editingRectForBounds:(CGRect)bounds; 

Here is the code:

Interface in MYTextField.h

 @interface MYTextField : UITextField @end 

Its implementation in MYTextField.m

 @implementation MYTextField static CGFloat leftMargin = 28; - (CGRect)textRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; return bounds; } - (CGRect)editingRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; return bounds; } @end 
+44
May 11 '11 at 12:45
source share
 UITextField * textField = [[UITextField alloc]init]; [textField setDelegate:self]; [textField setFrame:CGRectMake(170,112,140,25)]; [textField setBorderStyle:UITextBorderStyleNone]; [textField setBackgroundColor:[UIColor clearColor]]; [self.View addSubview:noofChildTField]; UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)] autorelease]; textField.leftView = paddingView; textField.leftViewMode = UITextFieldViewModeAlways; 

Try this code

+11
Mar 23 '13 at 6:11
source share

For Swift 3:

Exit the UITextField , say usernameTextField. Then write the following code in viewDidLoad()

 let paddingView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20)) usernameTextField.leftView = paddingView usernameTextField.leftViewMode = .always 

Change width: 5 to a larger value if more space is required.

+8
Jan 09 '17 at 12:00
source share

I was hoping to see the setting in IB in the Property inspector to set this value. It turns out that I inadvertently set the alignment and border style for values ​​that messed up with filling on the left.

You would not think that it would be very important to choose the right to the left and the absence of a border, but this makes the words mostly overlap or approach the edge of the window, and it looks wrong.

Poorly:

enter image description here

Good:

enter image description here

This is fixed for me. Hope this helps someone else too.

+5
Mar 09 '16 at 16:36
source share

i achieved almost by overriding - (CGRect)textRectForBounds:(CGRect)bounds . now the problem is when TextField goes into edit mode of its left margin reset to zero ...

 @implementation UITextField(UITextFieldCatagory) - (CGRect)textRectForBounds:(CGRect)bounds { CGRect theRect=CGRectMake(bounds.origin.x+10, bounds.origin.y, bounds.size.width-10, bounds.size.height); return theRect; } 
+1
Apr 15 '11 at 9:17
source share

Subclass UITextField and add the extension:

 extension UITextField { func paddingLeft(inset: CGFloat){ self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: inset, height: self.frame.height)) self.leftViewMode = UITextField.ViewMode.always } } 

usage

use of
 class MyUITextFieldClass: UITextField { override func awakeFromNib() { super.awakeFromNib() // Initialization code self.paddingLeft(inset: 10) } } 
+1
Aug 28 '19 at 1:23
source share

For Swift 4 :

I prefer to use IBDesignable and IBInspectable to allow me to set IBInspectable through Xcode storyboards and save it again. I also updated the code to work in Swift 4.

 import Foundation import UIKit @IBDesignable class PaddableTextField: UITextField { var padding = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0) @IBInspectable var left: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var right: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var top: CGFloat = 0 { didSet { adjustPadding() } } @IBInspectable var bottom: CGFloat = 0 { didSet { adjustPadding() } } func adjustPadding() { padding = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right) } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() } override func textRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } override func placeholderRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)) } } 
0
Dec 02 '18 at 3:46
source share

You can try this

 TextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; TextField.textAlignment = UITextAlignmentCenter; 
-3
Apr 15 '11 at 9:17
source share



All Articles