20150702_UI之UITextField使用

本文详细介绍如何在iOS应用中自定义UITextField的样式,并通过UITextFieldDelegate实现文本框的编辑控制。包括设置边框、提示文本、键盘类型等属性,以及实现开始编辑、结束编辑等代理方法。

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

<pre name="code" class="objc">ViewController遵守<span style="font-family: Arial, Helvetica, sans-serif;">UITextFieldDelegate协议</span>

//
//  ViewController.h
//  IOS150702_UI(02)_UITextField
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>


@end


//
//  ViewController.m
//  IOS150702_UI(02)_UITextField
//
//  Created by PengJunlong on 15/7/2.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width-40, 50)];
    textField.backgroundColor = [UIColor greenColor];
    //设置文本框边框的风格
//    typedef NS_ENUM(NSInteger, UITextBorderStyle) {
//        UITextBorderStyleNone,
//        UITextBorderStyleLine,
//        UITextBorderStyleBezel,
//        UITextBorderStyleRoundedRect
//    };
    textField.borderStyle = UITextBorderStyleRoundedRect;//圆角
    //设置默认提示文本,当编辑内容时,提示内容消失
    textField.placeholder = @"请输入内容";
    //设置文本框的初始内容
    textField.text = @"你好中国";
    //设置文本颜色
    textField.textColor = [UIColor redColor];
    //设置文本内容字体
    textField.font = [UIFont systemFontOfSize:25];
    //设置文本的对齐方式
    textField.textAlignment = NSTextAlignmentRight;
    //设置文本编辑时,是否清空文本内容
    textField.clearsOnBeginEditing = YES;
    //设置何时显示清除按钮
//    typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
//        UITextFieldViewModeNever,
//        UITextFieldViewModeWhileEditing,
//        UITextFieldViewModeUnlessEditing,
//        UITextFieldViewModeAlways
//    };
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;//编辑是显示清除按钮
    //设置左视图
    //设置IMageView的origin无效
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"player2"]];
    imageView.frame = CGRectMake(0, 0, 30, 30);
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = imageView;
    //设置右视图
    UIImageView *rightimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"player2"]];
    rightimageView.frame = CGRectMake(0, 0, 30, 30);
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = rightimageView;
    
    
    //设置垂直方向的文字对齐方式
    //typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
    //    UIControlContentVerticalAlignmentCenter  = 0,
    //    UIControlContentVerticalAlignmentTop     = 1,
    //    UIControlContentVerticalAlignmentBottom  = 2,
    //    UIControlContentVerticalAlignmentFill    = 3,
    //};
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    //设置水平方向的对齐方式
    //typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
    //   UIControlContentHorizontalAlignmentCenter = 0,
    //    UIControlContentHorizontalAlignmentLeft   = 1,
    //    UIControlContentHorizontalAlignmentRight  = 2,
    //    UIControlContentHorizontalAlignmentFill   = 3,
    //};
    textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //在模拟器上切换键盘 command + K
    //shift + command + k 开启电脑键盘
    
    //设置键盘的风格
    //设置键盘风格
    //UIKeyboardTypeDefault,       默认键盘,支持所有字符
    //UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘
    //UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
    //UIKeyboardTypeURL,           URL键盘,支持.com按钮 只支持URL字符
    //UIKeyboardTypeNumberPad,     数字键盘
    //UIKeyboardTypePhonePad,       电话键盘
    //UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名
    //UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘
    //UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点
    //UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符
    //UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
    textField.keyboardType = UIKeyboardTypeURL;
    
    //设置键盘return键风格
    //UIReturnKeyDefault,
    //UIReturnKeyGo,
    //UIReturnKeyGoogle,
    //UIReturnKeyJoin,
    //UIReturnKeyNext,
    //UIReturnKeyRoute,
    //UIReturnKeySearch,
    //UIReturnKeySend,
    //UIReturnKeyYahoo,
    //UIReturnKeyDone,
    //UIReturnKeyEmergencyCall,
    textField.returnKeyType = UIReturnKeyGo;
    
    UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, self.view.frame.size.width-40, 50)];
    secondTextField.backgroundColor = [UIColor yellowColor];
    secondTextField.borderStyle = UITextBorderStyleRoundedRect;
    //设置密码风格输入
    secondTextField.secureTextEntry = YES;
    //设置该UITextField为第一窗口响应者
    [secondTextField becomeFirstResponder];
    
    //设置ViewController作为TextField的代理,ViewController要遵循UITextFieldDelegate协议
    secondTextField.delegate = self;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width-40, 50)];
    label.backgroundColor = [UIColor grayColor];
    label.tag = 101;
    [self.view addSubview:label];
    [self.view addSubview:secondTextField];
    [self.view addSubview:textField];
}

//给代码做一个标签,方便我们找到该段代码
#pragma mark      UITextFieldDelegate
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"将要开始编辑");
    return YES; //返回NO则不能进行后续方法的调用,不会调用textFieldDidBeginEditing
}

//开始编辑文本框的时候调用这个方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"开始编辑");
    
}

// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"将要结束");
    return YES;
}
// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"结束编辑");
    UILabel *label = (UILabel *)[self.view viewWithTag:101];
    label.text = textField.text;
}

// called when 'return' key pressed. return NO to ignore.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"returnKey被点击");
    [textField resignFirstResponder];   //隐藏键盘
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

结果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值