UITextView input cursor issue in iOSsimulator - ios

I am trying to create dynamic UITextViewController for my iOS application.So from my ApplicationViewController I am trying to get the TextView.
Textview is coming properly but the input cursor for my UITextView in coming down.How to get the blue input cursor from the top?
TextViewController.m
+(UITextField *)prepareUITextView
{
UITextView *uiTextView= [[UITextView alloc] initWithFrame:CGRectMake(
10, 80, 300, 100)];
uiTextView.layer.borderWidth = 1.0;
uiTextView.layer.cornerRadius = 8;
uiTextView.font = [UIFont systemFontOfSize:15];
uiTextView.autocorrectionType = UITextAutocorrectionTypeNo;
uiTextView.keyboardType = UIKeyboardTypeDefault;
uiTextView.returnKeyType = UIReturnKeyDone;
[uiTextView becomeFirstResponder];
uiTextView.selectedRange = NSMakeRange(0, 0);
return uiTextView;
}
ApplicationViewController.m
[self.view addSubview:[TextViewController prepareUITextView]];

Try this code
uiTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Adjust the Top value the way you want. this will solve your issue.

Related

UITextField does not interact with user when added to a clipped view area

I created a UIView with an outlet named _waypointSubview inside a UIScrollView, which is also inside another UIView, and all 3 were created in my storyboard. From here I am adding UITextFields programmatically, and once there are over 5 UITextFields added it is clipped from the UIScrollView. The UIScrollView and the _waypointSubview are then resized to compensate for the extra UITextField added. The user can then scroll down to see all the UITextFields that were clipped from the view. Only the UITextFields that were clipped do not allow editing. I am not sure why that is. So if I add 10 of them, I cannot edit 6 thru 10. I tried programmatically telling it to enable user interaction using the call [waypointTextField setUserInteractionEnabled:YES];but it did not work.
Can someone please explain why I cannot edit those fields? Thank you.
- (IBAction)addWaypoint:(id)sender {
UITextField *waypointTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord, yCord, 250, 40)];
waypointTextField.borderStyle = UITextBorderStyleRoundedRect;
waypointTextField.font = [UIFont systemFontOfSize:18];
waypointTextField.placeholder = #"enter waypoint";
waypointTextField.autocorrectionType = UITextAutocorrectionTypeNo;
waypointTextField.keyboardType = UIKeyboardTypeDefault;
waypointTextField.returnKeyType = UIReturnKeyDone;
waypointTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
waypointTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
waypointTextField.delegate = self;
[_waypointSubview addSubview:waypointTextField];
UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
commentTextField.borderStyle = UITextBorderStyleRoundedRect;
commentTextField.font = [UIFont systemFontOfSize:18];
commentTextField.placeholder = #"enter comment";
commentTextField.autocorrectionType = UITextAutocorrectionTypeNo;
commentTextField.keyboardType = UIKeyboardTypeDefault;
commentTextField.returnKeyType = UIReturnKeyDone;
commentTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
commentTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
commentTextField.delegate = self;
[_waypointSubview addSubview:commentTextField];
// if waypointArray is not initialized yet, create and add textfield
if (waypointArray == nil) {
waypointArray = [NSMutableArray arrayWithCapacity:1];
[waypointArray insertObject:waypointTextField atIndex:0];
commentArray = [NSMutableArray arrayWithCapacity:1];
[commentArray insertObject:commentTextField atIndex:0];
}
else{
// check amount of space left in scroll view, and adjust if needed
if ([waypointArray count] > 4) {
self.waypointScrollView.contentSize = CGSizeMake(708, 255 + (([waypointArray count] - 4)*50));
CGRect frame = self.waypointSubview.frame;
frame.size.height += 50;
self.waypointSubview.frame = frame;
//[waypointTextField setUserInteractionEnabled:YES];
}
// add new text fields to corresponding arrays
[waypointArray addObject:waypointTextField];
[commentArray addObject:commentTextField];
}
yCord += 50; // update the yCoordinate that will be used for the next textField placement
}
I am assuming you need to connect _waypointSubview to the view controller on your storyboard by just reading the comments.
In order to do that
Right click on the the view controller icon on the storyboard view controller
Click and drag the little circle to the object you are trying to edit or the method you're trying to implement from the IBAction.
Check your bounds of your textfield as -
UITextField *commentTextField = [[UITextField alloc] initWithFrame:CGRectMake(xCord + 300, yCord, 380, 40)];
Here if you observe the xcordinate, an Iphone has a width of 320points.
If your textfield is 380points wide then how can you accomodate it WHEN its (0,0) is starting from xCord(I dont know what is its value) + 300.
Lower the xcordinate value and color its background, to check whether it falls in bounds as pointed out by #rdelmar

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.

Add UILabel as a constant field to left of UITextField

I am facing on problem, I want to have a static text in UITextField.
Text like "UserName:" should appear constantly to the left side of UITextField. Editing the UITextField should start from where the "UserName:" text ends.
For example, in iPhone settings, if we go to Twitter app and try to add new account. The way the user name and password textfield looks like. I want to develop same like that.
I don't understand why everyone has has posted such off the track answers to such a simple issue.
You simply do this:
Create a UILabel, with UI complimenting you textField.
You set it's text (Username:, etc).
Assign it to your textField's leftView property.
That's it. You will have to check frames, but that is basic, and I believe you can do that without much effort.
UPDATE: Sample code
UITextField* aField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
aField.placeholder = #"Please enter a username";
aField.layer.borderWidth = 1.0f;
aField.backgroundColor = [UIColor whiteColor];
aField.layer.borderColor = [UIColor lightGrayColor].CGColor;
aField.layer.cornerRadius = 3.0f;
UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 38)];
aLabel.text = #"Username:";
aLabel.font = aField.font;
aLabel.textColor = [UIColor grayColor];
aField.leftView = aLabel;
aField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:aField];
Only needs minor adjustments.
Try to use two text fields (one partially overlap with other) or Implement the below code.
-(void)viewDidAppear:(BOOL)animated{
yourTextField.text = #"UserName:";
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//handle back spaces and error conditions
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([editedText isEqualToString: #"UserName"])
{
return NO;
}
else
{
yourTextField.text = editedText;
return YES;
}
}
Below code may cater your need.
UIView *vis = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 50)];
UILabel *lblUserName = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 40)];
UITextField *txtUserName =[[UITextField alloc]initWithFrame:CGRectMake(125, 5, 180, 40)];
lblUserName.text= #"User Name";
txtUserName.placeholder = #"#User Name";
[vis addSubview:lblUserName];
[vis addSubview:txtUserName];
[vis setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:vis];
Regards,
Amit
Please try this code. I am sure this will help you.
UILabel *lblLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
lblLeft.backgroundColor = [UIColor clearColor];
lblLeft.text = #"Name: ";
lblLeft.textColor = [UIColor lightGrayColor];
lblLeft.font = [UIFont systemFontOfSize:14.0];
txtYourTextField.leftView = lblLeft;
Happy Coding :)
Deepak
For quick solution, do that with two overlapping textfields like this:
Then change the textfield border style to No border style and disable the user interaction of the below textfield:

Text is not properly visible in UITextField

UITextField *theLastNameTF = [[UITextField alloc]init];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
I'm creating a textfield using the above code.I need to show a line under the textfield,so i'm adding a gray coloured UILabel to text field.
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.size.height-0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}
I'm facing a issue in textfield,while entering texts,the text are half visible and cursor is not moving towards left of text.
Please refer the image,I have entered "arun... arun 123",as expected the cursor should be at left of 123,but the cursor is not moving further.
How can i fix this this?
EDIT
This code is to reframe the UI
(call layoutViewForCurrentorientation () inside orientation change method)
-(void)layoutViewForCurrentorientation
{
if ([[UIApplication sharedApplication]statusBarOrientation] == UIInterfaceOrientationPortrait) {
[self potraitUI];
}
else
{
[self landscapeUI];
}
}
If i remove the line textField.font = [UIFont fontWithName:#"Helvetica-neue" size:24.0]; .I issue is resolved but i need bigger font in textfield.
I was using font [UIFont fontWithName:#"Helvetica-neue" size:24.0] and the textfield size was 25.I made my UITextfield height to 30 and the issue got resolved.Don't know the actual reason but it worked.
Please set the textfield frame
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(50,50,250,30)];
I hope it helps you.
I'm seeing the same problem in 9.3. Appears to be an iOS bug.
When the UITextField is first responder, the UITextField contains a scrollview (UIFieldEditor) which contains the input view (a _UIFieldEditorContentView, -[UITextField inputView].) The bug is that the input view is the incorrect size. The input view is not wide enough and thus the scrollview's -contentSize is also incorrect. The UITextField is scrolling as far right as it can, but that's not far enough because the input view is too small.
My workaround is to reset the text field's text. You'll also probably need a flag to ignore delegate callbacks:
_ignoreTextDelegate = TRUE;
NSString *text = textField.text;
textField.text = nil;
textField.text = text;
_ignoreTextDelegate = FALSE;
Try this
UITextField *theLastNameTF = [[UITextField alloc]initWithFrame:CGRectMake(20,50,250,30)];
theLastNameTF.borderStyle = UITextBorderStyleNone;
theLastNameTF.font = [UIFont fontWithName:kHelveticaneue size:15.0];
theLastNameTF.textColor = [UIColor grayColor];
theLastNameTF.textAlignment = NSTextAlignmentLeft;
[theScrollView addSubview:theLastNameTF];
And set the line frame's y value should be with respect to textField frame's y value
-(void)addLineToTextField:(UITextField *)theTextfield
{
UILabel *theSeparatorLine = [[UILabel alloc]initWithFrame:CGRectMake(0, theTextfield.frame.origin.y + 0.5, theTextfield.frame.size.width, 0.5)];
theSeparatorLine.frame = CGRectIntegral(theSeparatorLine.frame);
theSeparatorLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
theSeparatorLine.backgroundColor = kCS_LightGrayColor;
[theTextfield addSubview:theSeparatorLine];
theSeparatorLine = nil;
}

Why doesn't the clear button aligned with the text in a UITextField?

I have an UITextField in an UITableViewCell.
For some reason the clear button isn't align to the textField's text.
Here's my textField code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(22, 10, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
usernameField.adjustsFontSizeToFitWidth = YES;
usernameField.placeholder = #"Username";
usernameField.backgroundColor = [UIColor clearColor];
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocorrectionTypeNo;
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.clearButtonMode = UITextFieldViewModeAlways;
[usernameField setEnabled:YES];
[cell addSubview:usernameField];
[usernameField becomeFirstResponder];
[usernameField release];
And here's the picture to demonstrate the problem:
Edit:
I also tried with - (CGRect)clearButtonRectForBounds:(CGRect)bounds and still nothing
What is your contentVerticalAlignment property? I've seen this issue show up when the clear button is center aligned but the text is aligned the top. Try setting your textfield alignment to the following:
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Any luck with that?

Resources