用户输入文本框控件(UITextField)与多行文本控件(UITextView)

本文介绍了iOS开发中,UITextField和UITextView的区别。UITextField主要用于单行文本输入,不支持换行,而UITextView则允许多行文本显示和换行。此外,文章还展示了UITextField的使用示例,并详细列举了UITextView的常用属性和协议方法,包括编辑状态变化的相关回调。

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

与UITextField控件相比,UITextView继承自UIScrollView:UIView类,它不仅可以输入并显示文本,而且可以在固定的区域展示足够多的文本,并且这些文本内容可以换行显示。

UITextField控件不能换行。

 1  #import "ViewController.h"

 2  @interface ViewController ()

 3  - (IBAction)login:(id)sender;//用于声明UIButton按钮的点击事件

 4  // 用于声明两个UITextField控件所对应的属性

 5  @property (weak, nonatomic)IBOutlet UITextField *password;

 6  @property (weak, nonatomic) IBOutletUITextField *username;

 7  @end

 8  @implementation ViewController

 9  - (void)viewDidLoad {

 10     [super viewDidLoad];

 11 }

 12 - (IBAction)login:(id)sender {

 13     // 获取用户名

 14     NSString *username=self.username.text;

 15     // 获取密码

 16     NSString *password=self.password.text;

 17     // 判断用户名和密码是否正确

 18     if ([username isEqualToString:@""]||[passwordisEqualToString:@""]) {

 19         [self showMessage:@"用户名或密码不能为空"];

 20     }else if(![password isEqualToString:@"12345"]||

 21                ![username isEqualToString:@"itcast"]){

 22         [self showMessage:@"用户名或密码错误"];

 23     }else if([username isEqualToString:@"itcast"]&&

 24                [password isEqualToString:@"12345"]){

 25        [self showMessage:@"登录成功"];

 26     }

 27 }

 28 // 提示信息的方法

 29 -(void)showMessage:(NSString *)message{

 30     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nilmessage:

 31                        message delegate:nil cancelButtonTitle:@"确定"

 32                        otherButtonTitles:nil,nil];

 33     [alert show];

 34 }

 35 @end



表2-1  UITextView的常见属性

属性声明

功能描述

@property(nonatomic,assign) id<UITextViewDelegate> delegate;

设置代理

@property(nonatomic,getter=isEditable) BOOL editable;

设置文本视图是否可编辑

@property(nonatomic,getter=isSelectable) BOOL selectable;

设置文本视图是否可选择

@property(nonatomic) BOOL clearsOnInsertion;

设置文本视图输入时是否清除之前的文本

@property(nonatomic,copy) NSAttributedString *attributedText;

设置文本视图默认插入的文字内容

@property (readwrite, retain) UIView *inputView;

设置底部弹出的视图

@property (readwrite, retain) UIView *inputAccessoryView;

设置底部弹出视图上方的辅助视图

@property(nonatomic) UIViewAutoresizing autoresizingMask;

设置文本视图自动适应高度

@protocol UITextViewDelegate<NSObject, UIScrollViewDelegate>

@optional

// 用户将要开始编辑UITextView的内容时会激发该方法

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView;

// 用户开始编辑该UITextView的内容时会激发该方法

-(void)textViewDidBeginEditing:(UITextView *)textView;

// 用户将要结束编辑该UITextView的内容时会激发该方法

-(BOOL)textViewShouldEndEditing:(UITextView *)textView;

// 用户结束编辑该UITextView的内容时会激发该方法

-(void)textViewDidEndEditing:(UITextView *)textView;

// 该UITextView内指定范围内的文本内容将要被替换时激发该方法

-(BOOL)textView:(UITextView *)textView

shouldChangeTextInRange:(NSRange)rangereplacementText:(NSString *)text;

// 该UITextView中包含的文本内容发生改变时会激发该方法

-(void)textViewDidChange:(UITextView *)textView;

// 用户选中该UITextView内某些文本时会激发该方法

-(void)textViewDidChangeSelection:(UITextView *)textView;

@end

从上述代码中可以看出,UITextViewDelegate协议中定义了很多方法,这些方法会在不同的状态下被激发。例如textView:shouldChangeTextInRange方法是替换多行文本控件中指定文本时会触发的方法,该方法可以实现把回车键当做退出键盘的响应键。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值