3

I would like to position the text in a textfield more specifically.

My current code:

UITextField * textFieldIndustry = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 465, 68)];
textFieldIndustry.font = [UIFont systemFontOfSize:17.0];  
textFieldIndustry.placeholder = @"Tap to..";  
textFieldIndustry.textAlignment = NSTextAlignmentCenter;

This code:

textFieldIndustry.textAlignment = NSTextAlignmentCenter; 

Centers the text but not exactly where I want it. How do I position using for example X & Y?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Torylon
  • 81
  • 1
  • 10

5 Answers5

2

You cannot set position of text implicitly in UITextField without subclassing it.

You can use UITextView instead, and should use the same approach with it:

UITextView *textView;
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
[textView setContentInset:insets];

You can use this approach also with subclassing the UITextField.

Community
  • 1
  • 1
art-divin
  • 1,635
  • 1
  • 20
  • 28
  • Thanks for the help. I fixed my problem by using a button instead of a textfield. The button allowed me to change the inset and still get the same result as using a textfield. – Torylon May 13 '13 at 12:31
0

You can also utilize the leftView portion of UITextView to do what you are asking. This is great for adding a prefix to a textField without placing a label in front of it. I'm not sure exactly how you wish to use it but it may be a good solution. Basically you just create a UILabel (or any view) with the width and height you like then add that as the leftView of the textField.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
0

Its very simple you just need to do textfield padding on which direction you want by using "UIEdgeInsets"

Ex: UIEdgeInsets insets = UIEdgeInsetsMake(0, 10, 0, 0); [textView setContentInset:insets]; It will show you the textfield after 10 pxls from .....like this you can give.

happy coding...

Satish
  • 133
  • 11
0
UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = @"Ghi chú";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
0

For the x, you can create a padding by adding a view to the left side of the textfield.

    let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: phoneNumberTextfield.frame.height))
    textFieldIndustry.leftView = paddingView
    textFieldIndustry.leftViewMode = .always

This ensures that the cursor is not at the edg

Paul Kahohi
  • 164
  • 1
  • 3
  • 6