Input Range Width Of UITextField iOS - 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;

Related

User can't able to enter amount before currency symbol in textfield in objective c

Below is an image of the screen, I just want to when the user enters the amount on this textfield then the user can't able to move cursor before $ sign and can't able to write amount before $ sign.
Please have a look on my problem, and give me idea about it,
You can simply add a textfield inside a view and before textfield add a lable with text "$". Remove the textfield border and add a border layer for view. It will work perfectly as per your requirement.
Try to set currency image as leftview of textfield
For Imageview
UIImageView *imageArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"currency.png"] ];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[paddingView addSubview:imageArrow];
imageArrow.center = paddingView.center;
textfield.leftView = paddingView;
textfield.leftViewMode = UITextFieldViewModeAlways;
For Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
label.text = #"$";
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;
You can use left view property of UITextField
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"$";
label.backgroundColor = [UIColor white];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightView = label;

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.

UISearchBar Layout Issues (iOS 8)

I'm making what should be a simple change to an app to add a UISearchBar component, but am getting some fairly strange layout quirks in the UI. It's perhaps easier to illustrate using pictures.
Here's what the component looks like when it doesn't have focus:
Note that the icon and the placeholder text are too far over, and the placeholder text is too far up. And here's what happens when I tap on the field:
The icon is now in the correct place, but the placeholder text is now too far to the left and overlapping with the icon. What gives?
When the field loses focus, it reverts to its original layout. Unless I type some text into it first, in which case it retains the second layout.
The code I'm using to set up the UISearchBar isn't doing anything special:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
containerView.backgroundColor = UI_COLOR_NAV_TITLEBAR;
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, containerView.frame.size.width - 40, 44)];
searchField.placeholder = #"search all items";
searchField.searchBarStyle = UISearchBarStyleMinimal;
searchField.showsBookmarkButton = NO;
searchField.showsCancelButton = NO;
searchField.showsScopeBar = NO;
searchField.showsSearchResultsButton = NO;
UITextField* textSearchField = [searchField valueForKey:#"_searchField"];
searchField.backgroundColor = [UIColor clearColor];
textSearchField.textColor = UI_COLOR_NAV_TEXT;
searchField.delegate = self;
[containerView addSubview:searchField];
//...
I want the UISearchBar to always appear as in the second example, except obviously without the text field overlapping with the icon. Is there a simple solution to this issue that I've overlooked?
Try following code for searchField..
[searchField setLeftViewMode:UITextFieldViewModeNever];
[searchField setRightViewMode:UITextFieldViewModeNever];
[searchField setBackground:[UIImage imageNamed:#"searchicon.png"]];
Last is to show icon at left side, use image for that.

UITextFields aren't allowing me to edit text

I've programmatically created two UITextFields in my iOS app, and set their text to the _minPrice and _minPrice variables, respectively.
The _minPrice and _maxPrice values appear correctly in the two fields, but tapping on them doesn't allow the user to edit them, they just remain those static values, backspacing doesn't work. Is there anything about my code thats preventing the text fields from being edited?
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view addSubview:view];
Your issue is clearly the frames you're setting...
Setting the color of the view you add the labels to to blue reveals your problem:
If you ensure that the labels are actually within the view you add them to (i.e. not negative), editing will be fine. All I did was change the negative x and y values in tf to positive, and they were editable:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];
Try this! Maybe there is another view at the top of the textField
your_textfield.userInteractionEnabled = YES;
If this doesn't work
add another line
[self.view bringSubviewToFront: your_textfield];
Try to add delegate methods on your textfields. Like
- (void) textFieldDidEndEditing:(UITextField *)textField
{
// ADD BREAKPOINT HERE.
}
Check if it goes to that line of code. If not maybe there's a view on top of it. Or you can try to bring textfield to front like .
[self.view bringSubviewToFront:yourTextfield];
But this isn't a good example of how you fix the problem. Just to test if there is a view on top of 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