用键盘输入,按下回车键
#import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *textInPut; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设置文本框的代理 self.textInPut.delegate = self; } //点击return键调用的方法 -(BOOL)textFieldShouldReturn:(UITextField *)textField { UILabel *textLabel = [[UILabel alloc]init]; [self.view addSubview:textLabel]; textLabel.text = self.textInPut.text; //清空输入框 self.textInPut.text = nil; //设置label的大小 [textLabel sizeToFit]; CGFloat w = textLabel.bounds.size.width; CGFloat h = textLabel.bounds.size.height; CGFloat x = [UIScreen mainScreen].bounds.size.width; CGFloat y = arc4random_uniform([UIScreen mainScreen].bounds.size.height -h); textLabel.frame = CGRectMake(x, y, w, h); //动画 [UIView animateWithDuration:5.0 animations:^{ textLabel.frame = CGRectMake(-w, y, w, h); }]; return YES; } @end