dyanmically add multiple UITextviews with new UITextView above the old UITextview - ios

I have a UITextView for adding comments to an article. Below this UITextView is a UIButton named 'Add Another Comment'. On Clicking this UIButton, another UITextView must be created such that the old UITextView gets pushed below the new one. I must be able to do this how many ever times i want.
EDIT: my problem is not creating the UITextView but pushing the old textview below the new one. This is how it should be:
a) Before opening up the comments section
b) After opening up the comments section

Hi John yes you can create uitextfield after clicking the button for that you have to write code as such below
- (IBAction)addText:(id)sender
{
CGRect textfieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textfieldFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
}
this link also use full for the reference

just change the y position of the old textview in animation block
[UIView animateWithDuration:1.0 animations:^{
// set center = btn.y+btn height+0.5*tetview height + any space below button if you want
}];
I have few doubts
Are using scroll view ? how user will see previous comments ? also what if the text height is larger than the height of textview ?

Related

UITextView input cursor issue in iOSsimulator

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.

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

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;
}

How do I set text in a TextField with a tag

I create programmatically 12 textfields using one method and I add a tag to each.
Below is the method that creates the textfields, this is called 12 times to create each of the textfields. This works.
textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = name;
[textField setTag:tag];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];
My question is how do I set the text in each textfield or how do I access it using the tags.
I tried:
[textField.2 setText: [NSString stringWithFormat : #"HEY"]];
and that does not work.
[(UITextField *)[self.view viewWithTag:2] setText:#"foobar"];
It's often a better idea to create an NSArray property that contains the text fields though.

Custom UITextField created programmatically doesnt bring up keyboard on tap

I have a custom look designed for my textfields on iphone. I decided to create a class
MYTextField : UIView <UITextFieldDelegate>
I have a method
- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position
that creates a UITextField with the desired view and look.
The sourcecode is as follows:
- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position {
if (self = [super initWithFrame:CGRectMake(position.x, position.y, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT)]) {
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)] autorelease];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textField setUserInteractionEnabled:YES];
textField.delegate = self;
[self addSubview:textField];
}
return self;
}
The problem is that, on invoking the method, the UITextField is visible but on tapping on the textfield, the keyboard doesnt show up.
Is there anything I am missing?
PS: I found a couple of similar problems on Stackoverflow, but none of the solutions seem to work for me. :(
The issue here could be that the textfields frame doesn't lie within the frame of the UIView. Why use TEXTFIELD_WIDTH for the self.frame but not for textfield.frame?
Also, why subclass UIView rather that UITextField?

Resources