文本框(UITextField)

文本框基础

(1)borderStyle属性:输入文本框的边框样式
(2)backgroundColor 属性: 设置输入框的背景颜色,使用其font属性设置字体
(3)clearButtonMode 属性: 设置一个清空按钮,通过设置clearButtonMode 可以指定是否以及何时显示清除按钮,(UITextFieldViewModeAlways不为空,或得焦点与没有焦点都显示清空按钮, UITextFieldViewModeNever不显示清空按钮,UITextFieldWhileEditing不为空, 且在编辑状态时显示清空按钮,UITextFieldModeUnlessEditing 不为空,且不在编辑状态时显示清空按钮)
(4)background 属性: 设置一个背景图片

textField.placeholder = @"请输入内容"   文本框中灰色的文字, 编辑时就会没有

textField1.borderStyle = UITextBorderStyleLine 或 UITextBorderStyleBezel 或  UITextBorderStyleRoundedRect 或   UITextBorderStyleNone  效果如下:

frame,  background, textcolor, textAlignment, font , text 一些属性





UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.

- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end

- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text


- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)

- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.



通知:

UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;

UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;

UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;


[[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidBeginEditingNotification object:nil userInfo:nil];


一些例子:

textFields_ = [[NSArray alloc] initWithObjects:textField1, textField2, textField3, textField4, nil];


- (void)textFieldDidBeginEditing:(UITextField*)textField {

  currentFieldIndex_ = [textFields_ indexOfObject:textField];

}


- (BOOL)textFieldShouldReturn:(UITextField*)textField {

  if ( textFields_.count <= ++currentFieldIndex_ ) {

    currentFieldIndex_ = 0;

  } 

  UITextField* newField = [textFields_ objectAtIndex:currentFieldIndex_];

  if ( [newField canBecomeFirstResponder] ) {

    [newField becomeFirstResponder];

  } 

  return YES;

}


背景图片

//导入背景图片,并设置成自动伸缩

  UIImage* imageWhitePaper = [UIImage imageNamed:@"paper.png"];

  UIImage* stretchableWhitePaper = [imageWhitePaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];

  UIImage* imageGrayPaper = [UIImage imageNamed:@"paperGray.png"];

  UIImage* stretchableGrayPaper = [imageGrayPaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];

  //创建UITextField实例

  UITextField* textField = [[[UITextField alloc] init] autorelease];

  textField.delegate = self;

  textField.frame = CGRectMake( 20, 100, 280, 50 );

  textField.background = stretchableWhitePaper;//设置背景图片


  textField.disabledBackground = stretchableGrayPaper;//一般是为nil, 如果没有设置背景

  textField.text = @"有图片";

  textField.textAlignment = UITextAlignmentCenter;

  textField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;

  [self.view addSubview:textField];



- (BOOL)textFieldShouldReturn:(UITextField*)textField {

  textField.enabled = NO;

  return YES;

}

没走此方法前  能编辑  有背景

走了此方法后  不能编辑  无背景



textField左右图片


UIImage* imageForLeft = [UIImage imageNamed:@"leftDog.png"];

  UIImageView* imageViewForLeft = [[[UIImageView alloc] initWithImage:imageForLeft] autorelease];

  UIImage* imageForRight = [UIImage imageNamed:@"rightDog.png"];

  UIImageView* imageViewForRight = [[[UIImageView alloc] initWithImage:imageForRight] autorelease];


  UITextField* textField1 = [[[UITextField alloc] init] autorelease];

  textField1.borderStyle = UITextBorderStyleRoundedRect;

  textField1.frame = CGRectMake( 20, 30, 280, 50 );

  textField1.text = @"一直在左右显示图片";

  textField1.textAlignment = UITextAlignmentCenter;

  textField1.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;//如何定位内容垂直内部控制   默认为中

  textField1.leftView = imageViewForLeft;//输入框左侧追加UIImageView

  textField1.rightView = imageViewForRight;//输入框右侧追加UIImageView

  textField1.leftViewMode = UITextFieldViewModeAlways;//让左侧UIView一直显示

  textField1.rightViewMode = UITextFieldViewModeAlways;//让右侧UIView一直显示

  [self.view addSubview:textField1];


  UITextField* textField2 = [[[UITextField alloc] init] autorelease];;

  textField2.borderStyle = UITextBorderStyleRoundedRect;

  textField2.frame = CGRectMake( 20, 100, 280, 50 );

  textField2.text = @"非编辑状态时右侧显示详细按钮";

  textField2.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;

  UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

  textField2.rightView = button;//输入框的右侧追加详细按钮

  textField2.rightViewMode = UITextFieldViewModeUnlessEditing;//只在非编辑模式下才显示

  [self.view addSubview:textField2];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡搜偶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值