Saturday, August 11, 2012

TextView General

extView is the same as TextField apart from TextView allowing multi-line editing.  A TextView’s Return key enters the newline character whereas a TextField’s return key dispatches the delegate method ‘textFieldShouldReturn’.

Example Of Use

Declaring in #ViewController.h file
        IBOutlet UITextView *MyTextViewName;
Releasing in #ViewController.m file
//*************************************
//*************************************
//********** VIEW DID UNLOAD **********
//*************************************
//*************************************

- (void)viewDidUnload
{
        [super viewDidUnload];

        [MyTextViewName release];
        MyTextViewName = nil;
}

//*****************************
//*****************************
//********** DEALLOC **********
//*****************************
//*****************************
- (void)dealloc
{
        [MyTextViewName release];

        [super dealloc];
}
Connecting in Interface Builder
Add the text filed to the view
Right click Files Owner
Drag the outlet to your text field

Storing Values Entered In A Text View

        [SomeIntVariable:[[SomeViewField text] intValue]];

Dismissing The Keyboard & Responding To Input

Open the XIB.
For each of the text views, right click and drag the ‘delegate’ circle to ‘Files Owner’

Then in #ViewController.m:
//************************************************
//************************************************
//********** TEXT VIEW CHECK FOR RETURN **********
//************************************************
//************************************************
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"])           //text is the new character being added
    {
        [textView resignFirstResponder];
        return FALSE;                                           //False so the /n doesn't get added
    }
    return TRUE;
}
If you have any methods in #ViewController.m that want to ensure the keyboard is removed:
//*****************************************
//*****************************************
//********** VIEW WILL DISAPPEAR **********
//*****************************************
//*****************************************
- (void)viewWillDisappear:(BOOL)animated
{
        [super viewWillDisappear:animated];

        [[self view] endEditing:YES];           //Ensure keyboard is removed
}

Dismissing the keyboard when the user touches anywhere else (but not on another object)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if ([textView isFirstResponder] && [touch view] != textView)
    {
        //The textView is currently being edited, and the user touched outside the text view
        [textView resignFirstResponder];
    }
}

Detect Entering A Text View

//***************************************
//***************************************
//********** TEXT VIEW ENTERED **********
//***************************************
//***************************************
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (textView == MyTextViewName)
    {
                if (DoSometingVariable)
                {
                        MyTextViewName.text = @"";
                }
    }
    return YES;
}

Set Text Colour

    //Set to RGB value
    myText.textColor = [UIColor colorWithRed:153/255.0 green:204/255.0 blue:51/255.0 alpha:1];




No comments:

Post a Comment