6

I want to make the placeholder text display in middle of the textfield (Padding placeholder text). The size of the placeholder text also needs to increase. My code is as follows, how can i solve this ?

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)];

textField.placeholder=@"iiiiiii";

UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110, 10, 0)];
textField.leftView = padView;
    textField.leftViewMode = UITextFieldViewModeAlways;


[self.view addSubview:textField];

UPDATE

enter image description here

I want the font size of the placeholder text to increase your name, and it should have a Left padding to it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

7 Answers7

13

You could subclass your UITextFiled and override methods:

MyTextField.m

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    return [self rectForBounds:bounds];
}

//here 40 - is your x offset
- (CGRect)rectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 40, 3);
}

upd: also set

textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

because it could some problems with io6 vs ios 7 vertical positionning

Evgeniy S
  • 1,464
  • 12
  • 32
4

Same question was being asked and answered as below Set padding for UITextField with UITextBorderStyleNone

  UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
  textField.leftView = paddingView;
  textField.leftViewMode = UITextFieldViewModeAlways;
Community
  • 1
  • 1
kamau wairegi
  • 428
  • 4
  • 6
2

You can set the starting text alignment (of the textField from xib or via code) to be center aligned.
Then in the -textFieldShouldBeginEditing, you can set the textField to be left aligned.
Similarly, on the -textFieldDidEndEditing, check if the textField is empty and if it is then set textField back to center aligned.

basically:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [textField setTextAlignment:NSTextAlignmentLeft];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField.text.length == 0) {
        [textField setTextAlignment:NSTextAlignmentCenter];
    }
}

EDIT::

the .h of your ViewController class should look like:

@interface ViewController : UIViewController <UITextFieldDelegate> 
{
    UITextField *myTextField;
}

now, replace your other code with this:

myTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 220,250,55)];

myTextField.placeholder=@"iiiiiii";

//important
[myTextField setDelegate: self];

//commented lines not really needed
//UIView *padView = [[UIView alloc] initWithFrame:CGRectMake(10, 110, 10, 0)];
//textField.leftView = padView;
//textField.leftViewMode = UITextFieldViewModeAlways;

[self.view addSubview:textField];
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • So do i need this `[textField setDelegate:self];` , When i add that line it says ` Sending 'ViewController *const __strong' to parameter of incompatible type 'id'` – Sharon Watinsan Dec 06 '13 at 15:07
  • @sharonHwk : yes, delegate is needed but **also**, in the **.h** of your `ViewController` you need to declare that your Class is using the delegate (*simply by **``***) ... example: `@interface ViewController : UIViewController ` – staticVoidMan Dec 06 '13 at 15:19
  • @sharonHwk : but... but... it's not exactly `[textField setDelegate:self];` ... in your `-viewDidLoad` or wherever you create the `UITextField` object, say something like `UITextField *txtFSomething = [UITextField alloc] init];` then you set delegate by `[txtFSomething setDelegate:self];` or you do it via the xib connection. – staticVoidMan Dec 06 '13 at 15:28
  • I still get the same warning. – Sharon Watinsan Dec 06 '13 at 15:43
  • @sharonHwk : where are adding the line `[textField setDelegate:self];` ? also, change the name of ou UITextField Object from `textField` to something else like `myTextField` and then do `[myTextField setDelegate:self];` – staticVoidMan Dec 06 '13 at 15:48
1
sampleTextfield.leftView = UIView(frame: CGRectMake(0, 0, 20, self.sampleTextfield.frame.height))
sampleTextfield.leftViewMode = UITextFieldViewMode.Always
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
Alvin George
  • 14,148
  • 92
  • 64
0

The easiest way that I found to do this task on swift 2 and Xcode 7:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let paddingView = UIView(frame: CGRectMake(0, 0, 25, self.emailTextField.frame.height))
        emailTextField.leftView = paddingView
        emailTextField.leftViewMode = UITextFieldViewMode.Always
    }
}
Jorge Luis Jiménez
  • 1,203
  • 13
  • 20
0
func placeholderPadding(textField:UITextField, leftPadding:CGFloat) {
            textField.leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: leftPadding, height: textField.frame.height))
            textField.leftViewMode = UITextFieldViewMode.always
        }
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
-3

While I have not specifically tested it, this should work:

self.YourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")

Add the amount of blank spaces (padding) to your string

Jeremy Kates
  • 38
  • 1
  • 4