您的位置:首页 > 产品设计 > UI/UE

UI - UITextFiledAndUIButton及应用的生命周期

2015-09-24 15:09 585 查看
<AppDelegate.m>

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

/*
//从给定的正方形的四角开始截取,显示出一个圆形
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 100, 100)];
view.backgroundColor = [UIColor redColor];
view.layer.cornerRadius = 50;  //这个数字是正方形的边长除以2,也是圆的半径
[self.window addSubview:view];
[view release];

*/

//=====================  UIFiled  =====================================

//UIFiled 是 UI 中常用的输入控件,也就是输入框,它和 UILabel 类似,也能进行文字的显示,仅仅多的是文字的编辑和输入功能,在使用上和 label 没有什么区别,仍然分四个步骤

// UITextFiled : UIControl : UIView : UIResponder : NSObject
//创建对象时,想使用已有的方法,从自身开始逐级向上级查找

//1.创建对象
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];

//2.配置属性
tf.backgroundColor = [UIColor yellowColor];
//(2)设置显示的文字
tf.text = @"duang~";
//(3)设置文字的颜色
tf.textColor = [UIColor redColor];
//(4)设置文字的对齐方式
tf.textAlignment = NSTextAlignmentCenter;
//(5)设置占位符 : 占位字符串(没有任何输⼊时,给出的提示字符串)
tf.placeholder = @"请输入字符";
//(6)设置输入框是否允许输入
tf.enabled = YES;  //默认是 YES, 是可以输入的 ,NO 是不可以输入的
//(7)设置输入框再次编辑时清空内容
tf.clearsOnBeginEditing = YES;   // 默认NO 是不清空的
//(8)设置进入安全模式
tf.secureTextEntry = NO;  // YES 即密码模式, 默认NO 为普通模式
//(9)设置键盘风格(枚举值)
tf.keyboardType =  UIKeyboardTypeDefault; //这个是默认的,还有其他纯数字键,网址输入,特殊符号输入等等
//(10)设置右下角return键样式(枚举值)
tf.returnKeyType =  UIReturnKeyGo;//这个显示前往.还有确认,谷歌,下一步,发送等等;
/*
*  键盘外观
UIKeyboardAppearanceDefault,  // Default apperance for the current input method.
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,
*/
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
//(11)设置输入视图(自定义弹出的代替键盘的视图)默认是键盘
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
inputView.backgroundColor = [UIColor greenColor];
tf.inputView = inputView;
[inputView release];
//(12)设置输入辅助视图 (输入视图上方的辅助视图)默认是 nil
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
view.backgroundColor = [UIColor redColor];
tf.inputAccessoryView = view;
[view release];
//(13)设置边框样式(枚举值)
tf.borderStyle = UITextBorderStyleRoundedRect;
//    tf.layer.cornerRadius = 10;  //用 layer 来控制边框外观
//(14)设置清除按钮的模式
tf.clearButtonMode = UITextFieldViewModeWhileEditing;//当编辑时显示清除按钮
//(15)设置左右视图
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
leftView.backgroundColor = [UIColor redColor];
tf.leftView = leftView; //将某视图或某标签或某图片放到左视图中
/**
*  UITextFieldViewMode
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
*/
tf.leftViewMode = UITextFieldViewModeAlways;//设置左视图类型(根据显示的时机定义)
//(16)设置键盘回收
tf.delegate = self;   //具体方法转下面第一个方法

/* 设置在什么的情况下自动大写
UITextAutocapitalizationTypeNone,             //除非自己点击大写,否则永不大写
UITextAutocapitalizationTypeWords,            //以单词来区分,每个单词首字母大写
UITextAutocapitalizationTypeSentences,        //以句子来区分
UITextAutocapitalizationTypeAllCharacters,    //所有字母全部大写
*/
tf.autocapitalizationType = UITextAutocorrectionTypeYes;
//设置为YES时文本会自动缩小以适应文本窗口大小,与minimumFontSize属性配合使用,与UILabel 中的类似.默认固定字体大小
tf.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
tf.minimumFontSize = 20;

//3.添加父视图
[self.window addSubview:tf];
//4.释放所有权
[tf release];

/**
*  重绘方法(用于重写在继承的子类中去改变某些位置或者属性)

- textRectForBounds:    //重写来重置文字区域
- drawTextInRect:         //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
- placeholderRectForBounds:  //重写来重置占位符区域
- drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
- borderRectForBounds:  //重写来重置边缘区域
- editingRectForBounds:  //重写来重置编辑区域
- clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
- leftViewRectForBounds:
- rightViewRectForBounds:
*/

//=====================  UIButton  =====================================

//UIButton 继承与 UIControl 是 UI 中长用得按钮控件,侧重于用户的剪辑事件响应操作.它和 label 在使用上也是类似的
//UIButton : UIControl : UIView : UIResponder : NSObject

//1.创建对象
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];  //UIButtonTypeCustom  自定义,UIButtonTypeSystem  系统的
//2.设置属性
bt.backgroundColor = [UIColor yellowColor];
//(2)设置 frame
bt.frame = CGRectMake(50, 100, 100, 50);
//(3)添加点击事件 : 添加的方法,可以有参数,但是参数最多只能有一个,并且参数就是当前的 button 对象
[bt addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];//点击下去弹起才会触发
[bt addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];//点击下去触发
//(4)设置指定状态下的标题
[bt setTitle:@"点我" forState: UIControlStateNormal];
[bt setTitle:@"高亮" forState: UIControlStateHighlighted];//表示点击下去不弹起的状态
//(5)获取指定状态下的标题
NSLog(@"%@",[bt titleForState:UIControlStateNormal]);
//(6)设置指定状态下 title 的颜色
[bt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[bt setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
//(7)获取指定状态下title的颜色
NSLog(@"%@",[bt titleColorForState:UIControlStateNormal]);
//(8)设置指定状态下的前景图片
[bt setImage:[UIImage imageNamed:@"bt.png"] forState:UIControlStateNormal];
//(9)设置指定状态下的背景图片
[bt setBackgroundImage:[UIImage imageNamed:@"tt.png"] forState:UIControlStateHighlighted];
//(10)获取指定状态下的前景图片
NSLog(@"%@",[bt imageForState:UIControlStateHighlighted]);
//(11)获取指定状态下的背景图片
NSLog(@"%@",[bt backgroundImageForState:UIControlStateHighlighted]);

//不可用状态是否调整图片,默认 yes
bt.adjustsImageWhenDisabled = YES;
//高亮状态是否调整图片,默认 yes
bt.adjustsImageWhenHighlighted = YES;
//是否显示点击时的淡光,默认 no
bt.showsTouchWhenHighlighted = YES;
//调整 title 的位置,默认居中
bt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//内嵌距离,调整图片或者 title 距离边缘上左下右的距离,默认UIEdgeInsetsZero
bt.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
bt.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
//设置内容部分,包括图片和title, 整体的设置,上面是分开设置的
bt.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);

//3.添加至父视图
[self.window addSubview:bt];

//4.释放所有权  [bt release];   !!!!!注:此步骤不需要,因为创建时用的UIButton 的便利构造器,没有 alloc 所以也不需要释放,若释放的话,程序会崩溃

//=====================  加法计算器  =====================================

UITextField *num1 = [[UITextField alloc] initWithFrame:CGRectMake(50, 80, 220, 50)];
num1.placeholder = @"输入数字";
num1.textAlignment = NSTextAlignmentCenter;
num1.borderStyle = UITextBorderStyleRoundedRect;
num1.keyboardType = UIKeyboardTypeNumberPad;
num1.tag = 100;
num1.delegate = self;
[self.window addSubview:num1];
[num1 release];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, 320, 50)];
label1.text = @"+";
label1.textAlignment = NSTextAlignmentCenter;
[self.window addSubview:label1];
[label1 release];

UITextField *num2 = [[UITextField alloc] initWithFrame:CGRectMake(50, 180, 220, 50)];
num2.placeholder = @"输入数字";
num2.textAlignment = NSTextAlignmentCenter;
num2.borderStyle = UITextBorderStyleRoundedRect;
num2.keyboardType = UIKeyboardTypeNumberPad;
num2.tag = 101;
num2.delegate = self;
[self.window addSubview:num2];
[num2 release];

UIButton *result = [UIButton buttonWithType:UIButtonTypeSystem];
result.frame = CGRectMake(130, 230, 60, 50);
[result setTitle:@"=" forState:UIControlStateNormal];
[result addTarget:self  action:@selector(caculate) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:result];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 280, 320, 50)];
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor grayColor];
label2.tag = 102;
[self.window addSubview:label2];
[label2 release];

return YES;
}

//UIFiled属性第(16)

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//将 textFiled 取消第一响应者
[textField resignFirstResponder];
return YES;
}

//UIButton 属性第(3)
-(void)clickButton:(UIButton *)button
{
NSLog(@"%@",[button titleForState:UIControlStateNormal]);
NSLog(@"Duang~");
}

-(void)click
{
NSLog(@"点击了一次");
}

//=====================  加法计算器  =====================================
-(void)caculate
{
UITextField *tf1 = (UITextField *)[self.window viewWithTag:100];
UITextField *tf2 = (UITextField *)[self.window viewWithTag:101];

int mul = [tf1.text intValue] + [tf2.text intValue];
UILabel *label = (UILabel *)[self.window viewWithTag:102];
label.text = [NSString stringWithFormat:@"%d",mul];
}

//程序将要取消活跃状态时触发
- (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.

NSLog(@"程序将要取消活跃状态  %s, %d",__FUNCTION__,__LINE__);

}

//程序进入后台触发的方法
- (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.
NSLog(@"程序进入后台触发的方法  %s  %d",__FUNCTION__,__LINE__);

}

//程序将要进入前台触发的方法
- (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.
NSLog(@"程序将要进入前台触发的方法  %s  %d",__FUNCTION__,__LINE__);
}

//程序已经进入活跃状态触发的方法
- (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.

NSLog(@"程序已经进入活跃状态触发的方法 %s  %d",__FUNCTION__,__LINE__);
}

//程序退出触发的方法
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

NSLog(@"程序退出触发的方法  %s  %d",__FUNCTION__,__LINE__);
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: