Position text in a textfield - ios

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?

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.

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...

UITextField *textFieldNote = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 310, 110)];
textFieldNote.placeholder = #"Ghi chĂș";
textFieldNote.textAlignment = NSTextAlignmentLeft;
textFieldNote.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

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

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.

Related

Input Range Width Of UITextField iOS

Is it possible to set a UITextField's input range (not the number of characters)? You can check out my screenshot below.
I already tried implementing this: Ellipsis at the end of UITextView
It didn't work, or I'm just doing it wrong.
What I want is to limit the size of the user's input AND make it ellipsis. The text at the left side of UITextfields are merely UILabels set as subviews.
So again... I need help in setting the range of the text input, and if the input length or width overlaps my RED MARK in my screenshot, the rest of the input will be converted to POINTS OF ELLIPSIS. And then I'm pretty sure I can now proceed to my project (example: clicking the arrow button will make a small popup that will show the full text).
I forgot to add my codes in that UITextField:
self.billingAddressTextField = [[UITextField alloc] init];
_billingAddressTextField.frame = CGRectMake(0, 150, scrollView.frame.size.width, 50);
_billingAddressTextField.delegate = self;
_billingAddressTextField.borderStyle = UITextBorderStyleNone;
_billingAddressTextField.background = [UIImage imageNamed:#"textfieldLogIn.png"];
[_billingAddressTextField setTextColor:[UIColor colorWithRed:0.686 green:0.686 blue:0.686 alpha:1.0]];
_billingAddressTextField.backgroundColor = [UIColor clearColor];
_billingAddressTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[_billingAddressTextField setFont:[_billingAddressTextField.font fontWithSize:16.0f]];
[_billingAddressTextField setTextAlignment:NSTextAlignmentRight];
[scrollView addSubview:_billingAddressTextField];
Ok solved, I just added a new Padding (UIView) at the left side of the UITextField (billingAddressTextField). So there are now two paddings (both side) in my UITextField.
UIView *paddingViewForArrowButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, 25)];
_billingAddressTextField.rightView = paddingViewForArrowButton;
_billingAddressTextField.rightViewMode = UITextFieldViewModeAlways;
UIView *paddingforBillingLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
_billingAddressTextField.leftView = paddingforBillingLeft;
_billingAddressTextField.leftViewMode = UITextFieldViewModeAlways;

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

UITextField in a TableViewCell doesn't appear

I want to make a TableView Cell with a Label and Textfield in it, but the Textfield doesn't appear.
Here is the init of the textfield, it's in a cell-class:
self.tfTitle = [[UITextField alloc]initWithFrame:CGRectMake(20, 70, 100, 20)];
self.tfTitle.borderStyle = UITextBorderStyleRoundedRect;
self.tfTitle.placeholder = #"Titel/Ort";
self.tfTitle.textAlignment = NSTextAlignmentRight;
self.tfTitle.adjustsFontSizeToFitWidth = YES;
[self addSubview:self.tfTitle];
try [self.contentView addSubview:self.tfTitle];
Also change your frame CGRectMake(20, 70, 100, 20) to CGRectMake(20, 10, 100, 20)
70 is too far from the top so that you cannot see it.
Since iOS7, UITableViewCell hierarchy has changed, consider adding the UITextField to self.contentView
[self.contentView addSubview:self.tfTitle];
It might not be the problem, but still, if you make it appear on self, you won't be able to select the textfield.
ALso make sure the frame you give it is in the cell (put some background color to the contentView of the self and to your textfield)

iOS :viewForHeaderInSection can't get align with cell?

I have this code in my viewForHeaderInSection: of tableViewController
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(20, 0, 320, 40)] autorelease];
l.backgroundColor = [UIColor clearColor];
//l.textAlignment = UITextAlignmentLeft; //i tried it didn't help!
l.text= NSLocalizedString(#"My Label:", #"");
the label always appears around 35 point off the cell's left side. If i use the UITextAlignmentCenter it works and it goes in center but I want it align with left size of cell. I tried the CGRectZero instead of CGRectMake(20, 0, 320, 40), changed x but it didn't work!
Any help greatly appreciated.
At first make view with frame (0, 0, 320, 40). Then put label on it.

Positioning UILabel inside a UITextView

Trying to position a UILabel above some text, but the elements are "stepping" on each other. I positioned them with x,y,h,w such that they should not do this but alas, they are. You can see in the screenshot that the label text "Abalone" and the text are commingled. What changes do I need to make here? I've already tried changing the x,y,w,h values to no great effect...
txtView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,280,300)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UILabel *wordTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
wordTitle.text = word.term;
[txtView addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
I think the problem is you are adding the label view as a subview to UITextview. You have to reserve some space for the label view in the UITextview. You can either add some padding to the textView, which can be confusing, because padding in UITextView is not constant and varies based on the font size. Or you can add your UILabel directly to the detailsViewController.view and start your UITextView below the label.
Hope it helps.

Resources