Accepting User Text Input with UITextField

textField在Interfa Builder中不能改变高度,但是在代码中可以,无论怎么改变其高度,行数始终是1

头文件

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UITextFieldDelegate>               //为了监听用户触发的事件,例如开始输入,完成输入等,text Field要有自己的代理,这个代理遵守协议

@property (nonatomic,strong) UITextField *textField;

@property (nonatomic,strong) UILabel *labelCounter;

 

@end

 

实现

#import "ViewController.h"

 

@interfaceViewController ()

 

@end

 

@implementation ViewController

 

- (void) calculateAndDisplayTextFieldLengthWithText: (NSString *) paramText {

    

    NSString *characterOrCharacters = @"characters";

    if (characterOrCharacters.length == 1) {

        characterOrCharacters = @"charater";

    }

    

    self.labelCounter.text = [NSString stringWithFormat:@"%lu %@", (unsigned long)paramText.length, characterOrCharacters];

}

 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    BOOL result = YES;

    if ([textField isEqual:self.textField]){

        NSString *wholeText =

        [textField.textstringByReplacingCharactersInRange:range

                                                withString:string];

        [selfcalculateAndDisplayTextFieldLengthWithText:wholeText];

    }

    return result;

}

//Keyboard消失

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

    returnYES;

}

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    CGRect textFieldFrame = CGRectMake(38.0f, 30.0f, 250.f, 23.f);

    self.textField = [[UITextField alloc] initWithFrame:textFieldFrame];

    self.textField.delegate = self;

    self.textField.borderStyle =UITextBorderStyleRoundedRect;

    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//默认左上

    self.textField.placeholder = @"Enter the text here…………";

    //self.textField.center = self.view.center;

    [self.view addSubview:self.textField];

    

    

    CGRect labelCounterFrame = self.textField.frame;

    labelCounterFrame.origin.y += textFieldFrame.size.height + 10;

    self.labelCounter = [[UILabel alloc] initWithFrame:labelCounterFrame];

    [self.viewaddSubview:self.labelCounter];

    

    [selfcalculateAndDisplayTextFieldLengthWithText:self.textField.text];

    UILabel *currencyLabel = [[UILabel alloc] initWithFrame:CGRectZero];

    currencyLabel.text = [[[NSNumberFormatter alloc] init] currencySymbol];

    currencyLabel.font = self.textField.font;

    [currencyLabel sizeToFit];

    self.textField.leftView = currencyLabel;

    self.textField.leftViewMode = UITextFieldViewModeWhileEditing;   //leftViewModerightViewMode两个property控制currencyLabel出现的形式,此时是用户输入text时出现

//    typedef enum { UITextFieldViewModeNever,UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways

//    } UITextFieldViewMode;

    

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

}

 

@end

                                              

Displaying Long Lines of Text with UITextView :keyboard不能将用户输入的text遮挡住

头文件

@interface ViewController : UIViewController

@property (nonatomic,strong) UITextView *myLongTextView;

 

@end

 

实现

#import "ViewController.h"

 

@interfaceViewController ()

 

@end

 

@implementation ViewController

- (void) handleKeyboardDidShow:(NSNotification *)paramNotification{

    /* Get the frame of the keyboard */

    NSValue *keyboardRectAsObject = [[paramNotification userInfo]    //userInfo是NSDictionary类型的一个属性,其中一个key                                                 是UIKeyboardFrameEndUserInfoKey,包括一个NSValue类型的keyboard的                                        boundaries,而这个boundaries放在一个CGRect中。

                                     objectForKey:UIKeyboardFrameEndUserInfoKey];

    /* Place it in a CGRect */

    CGRect keyboardRect;

    [keyboardRectAsObject getValue:&keyboardRect];

    /* Give a bottom margin to our text view that makes it reach to the top of the keyboard */

    self.myLongTextView.contentInset = UIEdgeInsetsMake(0.0f,

                                                    0.0f,

                                                    keyboardRect.

                                                    size.height,

                                                    0.0f);

}

- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{

    /* Make the text view as big as the whole view again */

    self.myLongTextView.contentInset = UIEdgeInsetsZero;

}

/*在- (void) viewWillAppear:方法中开始looking for keyboard的notifications, 而在- (void) viewWillDisappear:方法中停止监听。让你的view controller停止监听是很重要的,因为当你的view controller不被display,那你不会希望接收其他的view controller引发的keyboard notifications. */

- (void) viewWillAppear:(BOOL)paramAnimated{

    [super viewWillAppear:paramAnimated];

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                                      selector:@selector(handleKeyboardDidShow:)

                                                      name:UIKeyboardDidShowNotification

                                                     object:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:self

                        selector:@selector(handleKeyboardWillHide:)

                    name:UIKeyboardWillHideNotification

                          object:nil];

 

    self.view.backgroundColor = [UIColorwhiteColor];

    self.myLongTextView = [[UITextViewalloc] initWithFrame:self.view.bounds];

    self.myLongTextView.text = @"Some text here...";

    self.myLongTextView.font = [UIFontsystemFontOfSize:16.0f];

    [self.viewaddSubview:self.myLongTextView];

    }

 

- (void) viewWillDisappear:(BOOL)paramAnimated{

    [super viewWillDisappear:paramAnimated];

    [[NSNotificationCenterdefaultCenter] removeObserver:self];

    }

 

@end

 

 

 

 

 

 

 

 

<UITextFieldDelegate> 中都是可选的方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.

- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

 

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)

- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.

posted on 2013-08-25 17:04  (@_@)~  阅读(318)  评论(0编辑  收藏  举报