Objective-C Learning Notes - UITextField and UITextView

UITextField Attribute that often used:

  • borderStyle;

  • background;

  • backgroundColor;

  • clearButtonMode;

    • UITextFieldViewModeAlways;

    • UITextFieldViewModeNever;

    • UITextFieldViewModeWhileEditing;

    • UITextFiledViewModeUnlessEditing;


代码生成UITextField:

    //初始化
    UITextField *textField1 = [[UITextField alloc] init];
    //背景色
    textField1.backgroundColor = [UIColor blackColor];
    //加个图
    UIImage *imageT = [UIImage imageNamed:@"0109_1.jpg"];
    //拉伸一下
    UIImage *stretchableWhitePaper = [imageT stretchableImageWithLeftCapWidth:20 topCapHeight:50];
    //加载图片到输入框
    textField4.background = stretchableWhitePaper;
    //显示清楚按钮的模式
    textField1.clearButtonMode = UITextFieldViewModeNever;
    //字体颜色
    textField1.textColor = [UIColor redColor];
    //文本对齐方式
    textField1.textAlignment = NSTextAlignmentCenter;
    //字体大小
    textField1.font = [UIFont systemFontOfSize:36];
    //绘制
    textField1.frame = CGRectMake(20, 20, 280, 30);
    //边框类型
    textField1.borderStyle = UITextBorderStyleLine;
    //内容文本
    textField1.text = @"11111111111";
    //这啥?
    textField1.returnKeyType = UIReturnKeyNext;
    //添加到当前视图
    [self.view addSubview:textField1];




UITextView

    Base Attributes:

  •     Text

  • Placeholder

  • Background

  • Disabled

  • Align

  • Border Style

  • Clear Button

    • Never appears

    • Appears while edting

    • Appears unless edting

    • Is always visible

  • Clear  when edting begins

  • Text Color

  • Font

  • Min Font Size

  • Adjust To Fit

  • Captitalization

    • None

    • Words

    • Sentences

    • All Characters

  • Correction

  • Keyboard

  • Return Key

  • Auto-enable Return Key

  • Secure

  • textColor

  • font

  • editable

  • textAlignment

    • UITextAlignmentRight

    • UITextAlignmentCenter

    • UITextAlignmentLeft


动态实现:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize textView;
- (void)viewDidLoad {
    [super viewDidLoad];
    //new 一个实例
    textView = [[UITextView alloc] init];
    //frame设置
    textView.frame = self.view.bounds;
    //Mask大小自动
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    //不可编辑设置
    //textView.editable = NO;
    //字体大小
    textView.font = [UIFont systemFontOfSize:32];
    //内容
    textView.text = @"edit this!";
    //加载
    [self.view addSubview:textView];
    // Do any additional setup after loading the view, typically from a nib.
}

//加载 view 前干的事情
- (void)viewWillAppear:(BOOL)animated{
    //log 一下
    NSLog(@"viewWillAppear!");
    
    //调用 super 对应方法罗
    [super viewWillAppear:animated];
    //显示 navigationBar
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController setToolbarHidden:NO animated:YES];
    
}

//加载 view 之后干的事情
- (void)viewDidAppear:(BOOL)animated{
    //log 一下
    NSLog(@"viewDidAppear!");
    //调用 super 对应的方法罗
    [super viewDidAppear:animated];
    //调用 textViewDidEndEditing 方法
    [self textViewDidEndEditing:textView];
}

//view 隐藏之后干的事情
-(void)viewDidDisappear:(BOOL)animated{
    //调用 super 对应的方法
    [super viewDidDisappear:animated];
    //把 textView 注册为 firstResponser
    [textView resignFirstResponder];
}

//
- (void)textViewDidBeginEditing:(UITextView *)textView_{
    static const CGFloat kKeyboardHeight = 216.0;
    //设置navigationBar 左侧按键为[完成]
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePush)];
    //动画设置?
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    //缩小UITextView以免被键盘遮挡
    CGRect textViewFrame = textView_.frame;
    textViewFrame.size.height = self.view.bounds.size.height - kKeyboardHeight;
    textView_.frame = textViewFrame;
    //工具条上移,妈的太机智了
    CGRect toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
}

//同上差不多
-(void)textViewDidEndEditing:(UITextView *)textView_{
    //按键设置为[编辑]
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editDidPush)];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    
    //恢复UITextView的尺寸
    textView_.frame = self.view.bounds;
    //恢复工具条的位置
    CGRect toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
}

//两个尾巴方法
-(void)editDidPush{
    [textView becomeFirstResponder];
}
-(void)donePush{
    [textView resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

失败了别打我╮(╯﹏╰)╭……不知道为啥NavigationBar显示不出来,我需要找找源码看看……


标签:ios, object-c