1

So I am currently trying to add some space before my text in a text field. So when I type, it starts with four spaces instead of none. So I did some research and I came across something that seems really good (as indicated by all the votes), i.e., Create space at the beginning of a UITextField. One problem is that I do not know how to actually implement this in my other classes (assuming that's what the post is intending the reader to do).

This is what I think I'm supposed to do. I think I'm supposed to instantiate an object of that class and use the methods in the class to add spaces in front of my text field. But I don't actually know what that looks like in code. Could anyone give me an example of how to actually implement the code on this post? Create space at the beginning of a UITextField

Here is the code that I have so far:

import UIKit

class SignUpViewController: UIViewController {

    @IBOutlet weak var facebookButton: UIButton!
    @IBOutlet weak var googleplusButton: UIButton!
    @IBOutlet weak var fullNameTextField: UITextField!
    @IBOutlet weak var emailAddressTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        facebookButton.layer.cornerRadius = 5
        googleplusButton.layer.cornerRadius = 5

        fullNameTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        fullNameTextField.layer.borderWidth = 1.0
        emailAddressTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        emailAddressTextField.layer.borderWidth = 1.0
        passwordTextField.layer.borderColor = UIColor.lightGrayColor().CGColor
        passwordTextField.layer.borderWidth = 1.0

    }
}
Community
  • 1
  • 1
Jae Kim
  • 625
  • 3
  • 12
  • 23
  • you can set `LeftView` property in `TextField` to add some margin on left, same is for right side `RightView` alongwith it set visibility property of it. – Dipen Panchasara Sep 17 '15 at 07:02
  • Could you please give me an example? I know I don't set it to an integer and it has to be of type UIView... – Jae Kim Sep 17 '15 at 07:12

2 Answers2

1

You need to create subclass of UITextField. Then add this code in its implementation

static CGFloat leftMargin = 10;
- (CGRect)textRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;
    bounds.size.width -= (leftMargin + 20);
    return bounds;
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;
    bounds.size.width -= (leftMargin + 20);
    return bounds;
}

After that, set custom class to your UITextField.

ifau
  • 2,100
  • 1
  • 21
  • 29
Jugal K Balara
  • 917
  • 5
  • 15
  • 1
    Thank you for the response. I know how to set the sub class. But I'm not sure how to actually implement the subclass in my other classes. So in my SignUpViewController class, how would I implement this subclass? – Jae Kim Sep 17 '15 at 08:36
  • You need to create new class and set Subclass Type UITextfield then past code in .M file, after you don't need to implement subclass in your class. Then you ca select subclass from UITextfield property – Jugal K Balara Sep 17 '15 at 08:55
  • 1
    Oh I see... So it would be something like this? `fullNameTextField.textRectForBounds(CGRect)` But what goes in the parameter exactly? In other words, what do I put inside the parenthesis? – Jae Kim Sep 17 '15 at 09:23
1

You should create a class from @ScareCrow answer. After that go to the storyboard and change the class of the UITextField. Such as: Image

After that create an IBOutlet of the textfield. That outlet will be instance of the TextField class.

Community
  • 1
  • 1
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
  • So I don't have to do anything like, `fullNameTextField.textRectForBounds(CGRect)` or something? – Jae Kim Sep 18 '15 at 00:49
  • So what should I do after I create an instance of that class? There still isn't any space. I apologize for my noobness. I don't actually understand what is going in the code there. – Jae Kim Sep 18 '15 at 02:41
  • You should call the `NewBounds` method. As a parameter set your outlet bounds. That will return your new bounds. – ridvankucuk Sep 18 '15 at 06:16