//
// UITextFiledViewController.m
// AppUI组件学习
//
// Created by 麦子 on 15/6/17.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "UITextFiledViewController.h"
@interface UITextFiledViewController (){
UITextField *myField;
UITextField *myfield2;
}
@end
@implementation UITextFiledViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createView:self.view];
}
- (void)createView:(id)uiView{
UIView *view = (UIView *)uiView;
UITextField *textFiled = [[UITextField alloc] init];
textFiled.frame = CGRectMake(20, 80, 300, 100);
// UITextField 默认颜色是透明的
textFiled.backgroundColor = [UIColor whiteColor];
// 设置边框样式
textFiled.borderStyle = UITextBorderStyleRoundedRect;
textFiled.borderStyle = UITextBorderStyleLine;
textFiled.borderStyle = UITextBorderStyleNone;
textFiled.borderStyle = UITextBorderStyleBezel;
// 设置提示文字
textFiled.placeholder = @"请输入用户名";
// 设置密文输入
textFiled.secureTextEntry = YES;
// 设置键盘样式
textFiled.keyboardType = UIKeyboardTypeNumberPad;
// 设置键盘风格
textFiled.keyboardAppearance = UIKeyboardAppearanceAlert;
textFiled.keyboardAppearance = UIKeyboardAppearanceDefault;
textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
// 设置弹出视图----默认从下面出来,frame是没有用的。
UIImage *image = [UIImage imageNamed:@"tupian2.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 100, image.size.width, image.size.height);
textFiled.inputView = imageView;
// 设置左视图
UIView *leftView = [[UIView alloc] init];
leftView.backgroundColor = [UIColor yellowColor];
// 这里只有宽度才有效果
leftView.frame = CGRectMake(0, 0, 100, 100);
textFiled.leftView = leftView;
// // 设置左视图模式
textFiled.leftViewMode = UITextFieldViewModeAlways;
//
// // 设置又视图
UIView *rightView = [[UIView alloc] init];
rightView.backgroundColor = [UIColor yellowColor];
rightView.frame = CGRectMake(0, 0, 100, 100);
textFiled.rightView = rightView;
textFiled.rightViewMode = UITextFieldViewModeAlways;
// 设置清楚按钮模式---默认有个小插图片
textFiled.clearButtonMode = UITextFieldViewModeAlways;
myField = [[UITextField alloc] init];
myField.frame = CGRectMake(20, 200, 300, 100);
myField.borderStyle = UITextBorderStyleRoundedRect;
myField.backgroundColor = [UIColor whiteColor];
myField.text = @"设置清楚按钮模式---默认有个小插图片";
//再次编辑的时候,把原来的数据清空
myField.clearsOnBeginEditing = YES;
// 类容纵向对齐方式 还有横巷
// myField.contentVerticalAlignment = UIControlContentHorizontalAlignmentLeft;
myField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// myField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// 文字内容对齐
myField.textAlignment = NSTextAlignmentLeft;
// 滚动 -- (默认是滚动)
myField.adjustsFontSizeToFitWidth = NO;
// 设置最小字号
myField.minimumFontSize = 80;
// 设置首字母大写
myField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
// 设置return样式
myField.returnKeyType = UIReturnKeyGo;
// UITextField 协议方式
myField.delegate =self;
// 点击其他地方收起键盘
UIControl *myControl = [[UIControl alloc] init];
myControl.frame = [[UIScreen mainScreen] bounds];
myControl.backgroundColor = [UIColor redColor];
[myControl addTarget:self action:@selector(myControlClick) forControlEvents:UIControlEventTouchUpInside];
// 处理,当键盘收起或者是出现的时候,组件进行下移或者上移的功能实现
myfield2 = [[UITextField alloc] init];
myfield2.frame = CGRectMake(20, 500, 300, 100);
myfield2.backgroundColor = [UIColor whiteColor];
myfield2.borderStyle = UITextBorderStyleRoundedRect;
// 键盘出现和收到的系统通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWillShow) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWillHide) name:UIKeyboardDidHideNotification object:nil];
[view addSubview:myfield2];
[view addSubview:myField];
[view addSubview:textFiled];
[view addSubview:myControl];
[view sendSubviewToBack:myControl]; // 放在最下一层,事件也是有传导性的。 也有事件拦截(预测)
}
- (void)keyWillShow{
[UIView animateWithDuration:0.25 animations:^{
myfield2.frame = CGRectMake(20, 350, 300, 30);
} completion:^(BOOL finished) {
}];
}
- (void)keyWillHide{
[UIView animateWithDuration:0.25 animations:^{
myfield2.frame = CGRectMake(20, 500, 300, 100);
} completion:^(BOOL finished) {
}];
}
- (void)myControlClick{
NSLog(@"myControlClick");
[myField resignFirstResponder];
[myfield2 resignFirstResponder];
}
// 是否进入编辑模式 ,默认是YES
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
// 进入编辑模式
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"进入编辑模式");
}
// 是否可以退出编辑模式,默认是YES
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
// 退出编辑模式
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"退出编辑模式");
}
// 是否能够进行修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
// 是否可以点击清除按钮, 默认是YES
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
// 是否可以点击return键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
// 收键盘
[textField resignFirstResponder];
return YES;
}
@end
UITextField
最新推荐文章于 2024-01-27 12:25:06 发布