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

文本框(UITextField)

2015-09-05 22:32 465 查看

文本框基础

(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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: