Thursday, February 13, 2014

Insert Text in new line without deleting the previous text in Text View

When you want to add text in new line without deleting the previous text , you have to follow the steps below:
  • Assign the value of the textField in NSString type variable  say NSString*newText;
  • Assign the value of the textView in another NSString type variable say NSString*oldText;
  • Take a NSString type variable and append the value of your textField with new line @"\n" say NSString *myString;
  • Now append the value of myString and oldString and you well get your desired output. The Code is given below

  NSString *oldText,*newText,*myString;
- (IBAction)addNewItem:(id)sender {
    [self updateTextField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
   
    return YES;
}

-(void)updateTextField
{
    oldText=displayTextView.text;
    newText=entryTextField.text;
    myString = [newText stringByAppendingFormat:@"\n"];
    myString = [myString stringByAppendingString:oldText];
    displayTextView.text=myString;
   
   
}

The Screenshot of the application is provided below




Happy Coding
Mahboob