Creating custom UITextView with UILabels as well as text in it - iphone

I would like to create a custom UITextView with the ability to enter text in it, as well as programmatically adding some UILabels there that would act like text (I need to delete them with the 'backspace' button when the cursor comes near them).
This UITextView should be expandable and the labels can have different width.
Any ideas of how you can create this sort of thing, any tutorials or such?

you can create textfield using this code.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
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];
[textField release];
And for creating label you can use this code:
CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setText: #"My Label"];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor =[UIColor clearColor];
[view addSubview: label];
and for deleting label when backspace tape use this method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}
If the backspace button is pressed, then the replacementString(string) will have null value. So we can identify backspace button press using this.

Related

UITextField placeholder shifts when subclassed

I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.
self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = #"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;
I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.
UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
[txtPassword setPlaceholder:#"Password..."];
[txtPassword setBackgroundColor:[UIColor whiteColor]];
[txtPassword setFont:[UIFont fontWithName:#"Helvetica" size:15]];
[txtPassword setSecureTextEntry:YES];
[txtPassword setTextAlignment:NSTextAlignmentCenter];
[txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtPassword becomeFirstResponder];
Hope this helps.

Detecting backspace in UITextField the right way

Attention! This question is not a duplicate of this or this question! This is a new question to make things more clear.
So I have followed all the instructions of the posts above and I have this UITextField that becomes empty and moves to a new position every time the user hits return button. Moreover, I trace the input of the textView and I create a label of that text in the position the TextView was, like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField.text.length > 5) {
CGRect labelFrame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 0, 0);
UILabel *label = [[UILabel alloc] initWithFrame: labelFrame];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[label setText:textField.text];
[label setTextColor: [BSFunctions getColorFromHex:#"3f3f3f"]];
label.backgroundColor =[UIColor lightGrayColor];
[label sizeToFit];
[labelsArray addObject:label];
[self.view addSubview: label];
CGRect newTextFieldFrame = CGRectMake(labelFrame.origin.x + label.frame.size.width + 5, labelFrame.origin.y, 320, 30);
NSLog(#"Rect is %#", NSStringFromCGRect(newTextFieldFrame));
textField.frame = newTextFieldFrame;
textField.text = #"\u200B";
}
return YES;
}
I set the text in UITextField text to be #"\u200B" and I then want to detect the backspace button on it like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#""]) {
NSLog(#"backspace button pressed");
if (labelsArray.count > 0) {
UILabel *labelToDelete = [labelsArray lastObject];
CGRect labelPosition = labelToDelete.frame;
CGRect oldPosition = textField.frame;
textField.frame = CGRectMake(labelPosition.origin.x, labelPosition.origin.y, oldPosition.size.width, oldPosition.size.height);
[labelToDelete removeFromSuperview];
[labelsArray removeLastObject];
textField.text = #"\u200B";
}
}
return YES;
}
But the problem is, it works only once and then, even with the special character added at the beginning of the textField it doesn't work. What is possibly wrong?
After setting
textField.text = #"\u200B";
in textField:shouldChangeCharactersInRange:replacementString:
You will have to add
return NO;
otherwise it will continue to the return YES and replace it with a #"" and then when pressing the backspace once again nothing will be changed and the delegate method will not be called.
It is possible that I misunderstood your goal here, but I hope this helps.

how to add a textfield dynamically after clicking a button in iphone

How to add a textfields dynamically on clicking the button?
when clicking on the button textfield should appear with scrollview.
can anyone explain me how to do?
what have you tried??
May be this is what you are asking for.
-(IBAction)buttonClicked:(id)sender
{
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
}
EDIT
IF not dynamically,
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
[scrollView setHidden:YES];
}
-(IBAction)buttonClicked:(id)sender
{
[scrollView setHidden:NO];
}
You can use below code to add Text Field programmatically..
UITextField * aTextfield = [[UITextField alloc] initWithFrame:frame]; // give your frame
aTextfield.borderStyle = UITextBorderStyleRoundedRect;
aTextfield.textColor = [UIColor blackColor];
aTextfield.font = [UIFont systemFontOfSize:17.0];
aTextfield.placeholder = #"Suchen";
aTextfield.backgroundColor = [UIColor clearColor];
aTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
aTextfield.keyboardType = UIKeyboardTypeDefault;
aTextfield.returnKeyType = UIReturnKeySearch;
aTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
aTextfield.delegate = self;
[scrollview addSubview:aTextfield];
In Button action, you can create the dynamic textfields and them to your scrollview.....
Take a look at my code.
http://rajneesh071.blogspot.in/2012/09/make-textfield-and-label-dynamically-in.html
According to this you can create number of textfield and label in scroll view, so you can change it according to your requirement.

how to acces the textfield value created manually from a Click on Button

i am doing sample application where i am creating two TextField programitically i.e,Textfield1 and Textfield2 and Having one Button which is Dragged on UI.
Now My requirement is I have two textfield created manually and a Button, When i click a button it should access the value entered in both textfield and display its value on console using NSLog.
so can anyone suggest me how to do it?As textifeild is created programmitically it is not having IBOutlet. so i want to read textfield value when i click button and display on Console.so please suggest me with sample code
{
[self Textfiled1];
[Self TextField2];
}
-(void)textField1
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[self.view addSubview:textField];
[textField release];
}
Same as above for Textfield2.
Set tag for both the UITextField.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 1;
...
for the second,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.tag = 2;
...
In Button click action method,
UITextField *textField = (UITextField*)[self.view viewWithTag:1];
NSString *text1 = textField.text;
Similarly for the next UITextField.
Set the text field' s tag property before adding it and get the text field back using it.
textField.tag = 1;
UITextField* tf1 = (UITextField*)[self.view viewWithTag:1];
when you are adding textfield to self.view use the tag like below.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
textField.returnKeyType = UIReturnKeyDone;
textField.adjustsFontSizeToFitWidth = TRUE;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEndOnExit];
textField.tag = 3;
[self.view addSubview:textField];
[textField release];
whenever you need it then do like this.
UITextField *tex = [self.view viewWithTag:3];
NSLog(#"%#",tex.text);

how to create uitextfield dynamically using for loop

I try to create the uitextfield dynamically, but i can't create the textfield name.Anybody knows please help me.
from How do I create a text field?
To create a text field, use the UITextField class, as shown in Listing 11.
Listing 11: Creating a text field
CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
[textField setBorderStyle:UITextFieldBorderStyleBezel];
[textField setTag:1234];
[textField setTextColor:[UIColor blackColor]];
[textField setFont:[UIFont systemFontOfSize:20]];
[textField setDelegate:self];
[textField setPlaceholder:#"<enter text>"];
[textField setBackgroundColor:[UIColor whiteColor]];
textField.keyboardType = UIKeyboardTypeDefault;
Assign tag for each UITextField..
then access the textField using
UITextField *tf=(UITextField *)[yourView viewWithTag:tag];
for (int i=0; i<[checklistArray count]; i++) {
[self addUITextFieldMethod:i]
}
-(void) addUITextFieldMethod:(int)y {
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 70*y+40, 280, 31)];
textField.placeholder = #"Click here to type";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.textAlignment = NSTextAlignmentLeft;
textField.returnKeyType = UIReturnKeyDefault;
int DropDownTag = [[NSString stringWithFormat:#"10%d",y]integerValue];
textField.tag = DropDownTag ;
textField.delegate = self;
[textField addTarget:self action:#selector(returnFromKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[scrollViewChecklist addSubview:textField];
}
int textFieldTagValue = textField.tag;
/****************Code In Your Method****************/
UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTagValue];
myTextField.text = [arrayTest objectAtIndex:indexPath.row];