UI_UITextField

本文介绍了一个UITextField在iOS应用中的具体实现案例。通过AppDelegate类设置了UITextField的各种属性,如输入模式、清除行为、边框样式等,并实现了textFieldShouldReturn代理方法来响应键盘上的返回键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UITextField


AppDelegate.h

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>


@property (strong, nonatomic) UIWindow *window;



@end


AppDelegate.m

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (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];

    

    //创建TextField

    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 120, 180, 30)];

    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 120, 40, 30)];

    

    UIView *sssView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];

    sssView.backgroundColor = [UIColor yellowColor];

    //⾃自定义输⼊入视图(默认是键盘)

//    textField1.inputView = sssView;

    

//    输⼊视图上方的辅助视图(默认nil),要添加的辅助视图,可以不给宽度,但是必须要指定高度,宽度默认就是屏幕宽,可以弹广告

    textField1.inputAccessoryView = sssView;

    

    

    nameLabel.text = @"姓名:";

    nameLabel.textAlignment = NSTextAlignmentRight;

    textField1.text = @"king";

    

//    textField1.placeholder = @"joker";  //用来展位,不是内容

//    textField1.font = [UIFont systemFontOfSize:30];

    

    //加粗,字体大小

//    textField1.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];

    textField1.textAlignment = NSTextAlignmentLeft;

//textField1.textColor = [UIColor blueColor];

//  边框

    textField1.layer.borderWidth = 1;

    textField1.layer.cornerRadius = 5;

    

    //是否在输入的时候清空里面的内容,针对的时text属性的内容,不使placeholder

//    textField1.clearsOnBeginEditing = YES;   //清空的是text的内容,不是placeholder

    textField1.clearsOnBeginEditing = YES;


    textField1.enabled = YES;//是否能进行输入

    

    textField1.secureTextEntry = YES; //以密码的方式输入


//    textField1.keyboardType = UIKeyboardTypeEmailAddress;//键盘类型

//    textField1.returnKeyType = UIReturnKeySearch;     //键盘右下⾓角return按钮类型(枚举 )

    

    //输入框后边的x

    textField1.clearButtonMode = UITextFieldViewModeAlways;

    

    //设置代理人

    textField1.delegate = self;

    textField1.tag = 1000; //windowtag0

    

    [self.window addSubview:nameLabel];

    [self.window addSubview:textField1];

    [textField1 release];

    [nameLabel release];

    

//    NSLog(@"%p",textField1);


    [_window release];

    return YES;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //谁触发的协议方法,对应传过来的textField就是谁

//    NSLog(@"%ld", textField.tag);

//    NSLog(@"%@", textField.text);

//    NSLog(@"%p",textField);

//    textField.text = @"aaa";

    [textField resignFirstResponder];  //点击return回收键盘

    return YES;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值