UItextFile常用操作设置

@implementation AppDelegate
{
    UITextField *_field;
    UIButton *button;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(10, 30, 300, 30);
//    UITextBorderStyleNone,
//    UITextBorderStyleLine,  // 黑色线条框,内部颜色不是透明的
//    UITextBorderStyleBezel, // 灰色的框,内部也是透明的
//    UITextBorderStyleRoundedRect  圆角输入框,内部颜色白色
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //textField.backgroundColor = [UIColor redColor];
    // 文字提示
    textField.placeholder = @"请输入密码";
    textField.secureTextEntry = YES;
//    UIKeyboardTypeDefault,
//    UIKeyboardTypeASCIICapable,
//    UIKeyboardTypeNumbersAndPunctuation,
//    UIKeyboardTypeURL,
//    UIKeyboardTypeNumberPad,
//    UIKeyboardTypePhonePad,
//    UIKeyboardTypeNamePhonePad,
//    UIKeyboardTypeEmailAddress,
//    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),  
//    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),
//    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
    // 设置键盘样式
    textField.keyboardType = UIKeyboardTypeASCIICapable;
//    UIKeyboardAppearanceDefault,
//    UIKeyboardAppearanceDark
//    UIKeyboardAppearanceLight
//    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // Deprecated
    // 设置键盘风格
    textField.keyboardAppearance = UIKeyboardAppearanceAlert;
    // 设置弹出视图
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image/backrground.png"]];
    image.frame = CGRectMake(0, 100, 320, 100);
    // 再次编辑时是否清空之前的内容
    textField.clearsOnBeginEditing = YES;
    // 自己订阅键盘的样式
    textField.inputView = image;
    // 设置左视图
    UIView *leftView = [[UIView alloc]init];
    leftView.frame = CGRectMake(0, 0, 100, 100);
    leftView.backgroundColor = [UIColor orangeColor];
    textField.leftView = leftView;
//    UITextFieldViewModeNever,
//    UITextFieldViewModeWhileEditing,
//    UITextFieldViewModeUnlessEditing,
//    UITextFieldViewModeAlways
    textField.leftViewMode = UITextFieldViewModeAlways;
    UIView *rightView = [[UIView alloc]init];
    rightView.frame = CGRectMake(0, 0, 100, 100);
    rightView.backgroundColor = [UIColor greenColor];
    //textField.rightView = rightView;
    textField.rightViewMode = UITextFieldViewModeAlways;
    // 设置清除按钮模式
    textField.clearButtonMode = UITextFieldViewModeAlways;
    // 是否设置清空之前输入的内容
    textField.clearsOnBeginEditing = YES;
    
    [self.window addSubview:textField];
    [image release];
    [textField release];
    
    _field = [[UITextField alloc]init];
    _field.frame = CGRectMake(10, 150, 300, 30);
    _field.borderStyle = UITextBorderStyleRoundedRect;
    // 再次输入清空之前的内容
    _field.clearsOnBeginEditing = YES;
//    UIControlContentHorizontalAlignmentCenter = 0,
//    UIControlContentHorizontalAlignmentLeft   = 1,
//    UIControlContentHorizontalAlignmentRight  = 2,
//    UIControlContentHorizontalAlignmentFill   = 3,
    // 横向纵齐
    _field.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    // 垂直对齐方式
    _field.contentVerticalAlignment = UIControlContentHorizontalAlignmentFill;
    // 文本对齐方式
    _field.textAlignment = NSTextAlignmentCenter;
    // 设置textField滚动
    _field.adjustsFontSizeToFitWidth = YES;
    // 设置最小字号
    _field.minimumFontSize = 50;
    // 设置首字母是否大写
//    UITextAutocapitalizationTypeNone,
//    UITextAutocapitalizationTypeWords, 每一个字母首字目大写
//    UITextAutocapitalizationTypeSentences,  一行句子的首字母单词大写
//    UITextAutocapitalizationTypeAllCharacters, // 所有单词都大写
    _field.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    // 自动纠正
    _field.autocorrectionType = UITextAutocorrectionTypeYes;
    // 设置return 键样式
    _field.returnKeyType = UIReturnKeyRoute;
    // 设置代理
    _field.delegate =self;
    _field.clearButtonMode = UITextFieldViewModeUnlessEditing;
    _field.clearButtonMode = UITextFieldViewModeAlways;
    
    // UIControl *control = [[UIControl alloc]init];
    // control.frame = CGRectMake(0, 0, 320, 30);
    // [control addTarget:self action:@selector(controlClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.window addSubview:_field];
    // [self.window addSubview:control];
    // [self.window sendSubviewToBack:control];
    // [control release];
    
    button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 400, 300, 30);
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    self.window.backgroundColor = [UIColor blueColor]; // 影响的是输入框内部的颜色
    // 订阅键盘升起的系统通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
    // 订阅键盘收起的系统通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwillHide) name:UIKeyboardWillHideNotification object:nil];
    [self.window addSubview:button];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)dealloc
{
    [button dealloc];
    [_field dealloc];
    [super dealloc];
}
- (void)keyboardWillShow
{
    [UIView animateWithDuration:0.25 animations:^{
        button.frame = CGRectMake(10, 220, 300, 30);
    }completion:^(BOOL finished) {
        
    }];
}
- (void)keyboardwillHide
{
    [UIView animateWithDuration:0.25 animations:^{
        button.frame = CGRectMake(10, 400, 300, 30);
    }completion:^(BOOL finished) {
        
    }];
}
- (void)btnClick
{   // 方法 2
    [_field resignFirstResponder]; // 一点击就退出键盘(control控制键盘)
}
- (void)controlClick
{   // 方法 2
    [_field resignFirstResponder]; // 一点击就退出键盘(control控制键盘)
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{   // 是否应该进入编辑模式
    return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{  // 将进入编辑模式
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{   // 点击return收起键盘(方法 1)
    [textField resignFirstResponder];  // 收键盘
     return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{  // 进入编辑模式
    NSLog(@"进入编辑模式是调用");
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
// return NO to not change text
//
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @""; // 手动清除
    return NO;
}
// called when clear button pressed. return NO to ignore (no notifications)
//- (BOOL)textFieldShouldReturn:(UITextField *)textField;
// called when 'return' key pressed. return NO to ignore.

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值