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

UI一揽子计划 2 (UITextField  UIButton)

2015-09-10 12:48 483 查看
UItextField
一、创建一个UITextField时候默认就弹出键盘
    1

对齐方式
    textField.textAlignment

=
NSTextAlignmentCenter;
    2

是否允许输入
    textField.enabled

=
YES;
    3

是否清空输入框
    textField.clearsOnBeginEditing

=
YES;
    4

是否密文  NO

的时候是明文 

原点的颜色跟字体的颜色一致
    textField.secureTextEntry

=
YES;
    5

键盘的弹出格式
   

textField.keyboardType = UIKeyboardTypeNumberPad;
    6

回车return键的设置
    textField.returnKeyType

=
UIReturnKeyNext;
    7

自定义输入键盘视图
   

textField.inputView = inputView;  //
只有最后一个参数有用,最后一个参数用来控制高度
    8

设置自定义键盘的那一条横幅
   

textField.inputAccessoryView = inputAccessView;
    9

边框样式   

圆角
    textField.borderStyle

=
UITextBorderStyleRoundedRect;
    10

清除按钮
    textField.clearButtonMode

=
UITextFieldViewModeWhileEditing;

//
当开始编辑的时候就显示清除按钮
    11

左视图、右视图
   

UIView
*leftView = [[UIView

alloc]initWithFrame:CGRectMake(100,

100,

100,

100)];

    leftView.backgroundColor

= [UIColor

greenColor];
 

  textField.leftView = leftView;
 

  textField.leftViewMode = UITextFieldViewModeWhileEditing;
 

  textField.rightView = leftView;
 

  textField.rightViewMode = UITextFieldViewModeWhileEditing;
二、  使用协议中的方法的步骤
     1.遵循协议

     2.实现方法
     3.设置代理
//

遵循协议
               @interface

AppDelegate :
UIResponder
<UIApplicationDelegate,UITextFieldDelegate>
 
//

设置代理
        field.delegate

=
self;
//实现方法
- (BOOL)textFieldShouldReturn:(UITextField
*)textField
     {
取消第一响应者

回收键盘
 
[textField resignFirstResponder];

     *  1.判断第一个是不是第一相应者

        2.如果是就让第二个变成第一响应者

        3.如果点的是第二个就让第三个变成第一响应者

        4.如果是第三个就回收键盘
 
 if

(textField.tag

==
100
&& [textField
isFirstResponder] ==

YES) {
       

NSLog(@"点的是第一个,将第二个成为第一响应者。");

       
UITextField
*f1 = (UITextField

*)[self.window

viewWithTag:101];

        [f1
becomeFirstResponder];

    }
else
if
(textField.tag

==
101
&& [textField
isFirstResponder] ==

YES) {

       
NSLog(@"点的是第二个,将第三个成为第一响应者。");

       
UITextField
*f1 = (UITextField

*)[self.window

viewWithTag:102];

        [f1
becomeFirstResponder];

    }
else
if
(textField.tag

==
102
&& [textField
isFirstResponder] ==

YES) {

       
NSLog(@"点的是第三个,回收键盘");

        [textField
resignFirstResponder];

    }

   
return
YES;
}

  

/**

     * 
第一响应者

       
如果取消第一响应者 

键盘回收

     */

   
/**

     - (UIResponder*)nextResponder;

     - (BOOL)canBecomeFirstResponder;    // default is NO

     - (BOOL)becomeFirstResponder;

     - (BOOL)canResignFirstResponder;    // default is YES

     - (BOOL)resignFirstResponder;

     - (BOOL)isFirstResponder;
     */

UIButton
     * 

创建一个Button,

一般不适用init方法 

使用类方法

       
注意:不用管理内存

1   UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100,
100, 100, 100)];
   

UIButton
*button = [UIButton

buttonWithType:UIButtonTypeCustom];

// UIButtonTypeCustom 
自定义类型的

    button.frame

=
CGRectMake(100,

100,

100,

100);

    button.backgroundColor

= [UIColor

greenColor];
2设置点击的方法
    [button

addTarget:self

action:@selector(actionButton:)

forControlEvents:UIControlEventTouchUpInside];

   
3三种状态 

普通 

高亮 

选中
    [button

setTitle:@"高亮"

forState:(UIControlStateHighlighted)];

    [button
setTitle:@"普通"

forState:(UIControlStateNormal)];

    [button
setTitle:@"选中"

forState:(UIControlStateSelected)];

    [button
setTitleColor:[UIColor

blackColor]

forState:UIControlStateHighlighted];

    [button
setTitleColor:[UIColor

orangeColor]

forState:UIControlStateNormal];

    [button
setTitleColor:[UIColor

blueColor]

forState:UIControlStateSelected];
4 设置按钮 

背景图片    //

一般都使用背景图片
5 创建一张图片
   

UIImage
*image = [UIImage

imageNamed:@"Normal"];//

如果不是png

格式就要写全称
    [button
setBackgroundImage:image forState:(UIControlStateNormal)];
   

UIImage
*image1 = [UIImage

imageNamed:@"Selected"];
 

  [button setBackgroundImage:image1 forState:(UIControlStateSelected)];
   

UIImage
*image2 = [UIImage

imageNamed:@"HightLighted"];
 

  [button setBackgroundImage:image2 forState:(UIControlStateHighlighted)];
   
6设置前景图片
    [button

setImage:image

forState:UIControlStateNormal];

    [button
setImage:image1

forState:UIControlStateSelected];

    [button
setImage:image2

forState:UIControlStateHighlighted];
7取出各种状态下的标题
    NSLog(@"%@",
[button
titleForState:(UIControlStateNormal)]);
8取出各种状态下的图片
   
UIImage *image3 = [button imageForState:(UIControlStateNormal)];

9 button.backgroundColor = [UIColor yellowColor];
   

设置button的选中状态   

高亮状态中有普通状态才能切换过去
   

选中未选中之间切换
    if ([button isSelected]) {
        button.selected = NO;
    } else {
        button.selected = YES;
    }
10 与上面的代码是一样的。
    button.selected

= !button.selected;
11 . iOS 后台运行 (小退出和大退出的方法分别调用了那个方法)
- (void)applicationWillResignActive:(UIApplication
*)application {
   

NSLog(@"1%s%d",

__FUNCTION__,__LINE__);

   
NSLog(@"程序小退出状态。");

   
// 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.
// 当应用程序即将从活跃到不活跃的状态。这可能发生某些类型的暂时中断(如传入的电话或短信)或当用户退出应用程序,它开始过渡到后台状态。
// 使用这种方法暂停正在进行的任务,禁用计时器,和节流OpenGL ES帧率。游戏应该使用这种方法暂停游戏。
}

- (void)applicationDidEnterBackground:(UIApplication

*)application {

   
NSLog(@"2%s%d",

__FUNCTION__,__LINE__);

   
NSLog(@"程序进入后台挂起状态");

   
// 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.
  // 使用这种方法释放共享资源,保存用户数据,对计时器无效,存储足够多的应用程序状态信息来恢复您的应用程序的当前状态,以防它终止。
  // 如果您的应用程序支持后台执行,当用户退出时调用此方法代替applicationWillTerminate:。
}

- (void)applicationWillEnterForeground:(UIApplication

*)application {

   
NSLog(@"3%s%d",

__FUNCTION__,__LINE__);

   
NSLog(@"进入前台。");
   

// 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 {

   
NSLog(@"4%s%d",

__FUNCTION__,__LINE__);

   
NSLog(@"进入激活状态");
   

// 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 {

   
NSLog(@"5%s%d",

__FUNCTION__,__LINE__);

   
NSLog(@"程序意外退出");
   

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// 调用应用程序时终止。如果适当的保存数据。看到也applicationDidEnterBackground:。
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication

*)application

{

   
NSLog(@"内存发出警告");
}

--------------------------------------------------------------------------

一、键盘风格
UIKit框架支持8种风格键盘。

typedef enum {
UIKeyboardTypeDefault, //
默认键盘:支持所有字符
UIKeyboardTypeASCIICapable, //
支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, //标准电话键盘,支持+*#等符号

UIKeyboardTypeURL, UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, UIKeyboardTypeNamePhonePad, UIKeyboardTypeEmailAddress,

} UIKeyboardType;

用法用例:

// URL键盘,有.com按钮;只⽀支持URL字符
//数字键盘

//
电话键盘
//
电话键盘,也⽀支持输⼊人名字

//
用于输入电子邮件地址的键盘

textView.keyboardtype = UIKeyboardTypeNumberPad;

二、键盘外观

typedef enum { UIKeyboardAppearanceDefault, UIKeyboardAppearanceAlert,

} UIKeyboardAppearance;

用法用例:

//
默认外观:浅灰色
//深灰/石墨色

textView.keyboardAppearance=UIKeyboardAppearanceDefault;

三、回车键

typedef enum {
UIReturnKeyDefault, //默认:灰色按钮,标有Return
UIReturnKeyGo, //标有Go的蓝色按钮
UIReturnKeyGoogle, //标有Google的蓝色按钮,⽤于搜索
UIReturnKeyJoin, //标有Join的蓝色按钮
UIReturnKeyNext, //标有Next的蓝色按钮
UIReturnKeyRoute, //标有Route的蓝色按钮
UIReturnKeySearch, //标有Search的蓝色按钮
UIReturnKeySend, //标有Send的蓝色按钮
UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
UIReturnKeyDone, //标有Done的蓝色按钮
UIReturnKeyEmergencyCall, //紧急呼叫按钮

} UIReturnKeyType;

用法用例:

textView.returnKeyType=UIReturnKeyGo;

四、自动大写

typedef enum {
UITextAutocapitalizationTypeNone, //不⾃自动大写
UITextAutocapitalizationTypeWords, //单词首字⺟大写

UITextAutocapitalizationTypeSentences, //句子首字母大写

UITextAutocapitalizationTypeAllCharacters, //所有字母大写
} UITextAutocapitalizationType;

用法用例:

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

五、自动更正

typedef enum { UITextAutocorrectionTypeDefault,//默认
UITextAutocorrectionTypeNo,//不自动更正
UITextAutocorrectionTypeYes,//自动更正

} UITextAutocorrectionType;

用法用例:

textField.autocorrectionType = UITextAutocorrectionTypeYes;

六、安全⽂文本输入

textView.secureTextEntry=YES;
开启安全输⼊入主要是用于密码或⼀一些私⼈人数据的输⼊入,此时会禁用自动更正和自此缓存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: