1.UITextField的初始化和设置
textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
[textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
//如果出现UITextField不显示的情况,很可能是没设置其BorderStyle
textField.placeholder = @"password"; //默认显示的字
textField.secureTextEntry = YES; //密码
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
textField.delegate = self;
2.要实现的Delegate方法,关闭键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.textField resignFirstResponder];
return YES;
}
3. 可以在UITextField使用下面方法,按return键返回
-(IBAction) textFieldDone:(id) sender
{
[textFieldName resignFirstResponder];
}
链接TextField控件的"Did end on exit"
最右侧加图片是以下代码,
UIImageView *imgv=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=imgv;
text.rightViewMode = UITextFieldViewModeAlways;
如果是在最左侧加图片就换成:
text.leftView=imgv;
text.leftViewMode = UITextFieldViewModeAlways;
UITextField 继承自 UIControl,此类中有一个属性contentVerticalAlignment
所以想让UITextField里面的text垂直居中可以这样写:
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
查看函数的方法:
按住command键双击进入函数声明
按住alt键双击进入doc文档
///////////////////////////////////////////////////////////////
文本框常用方法:
如何用程序删除文本框中选中的文本
[textView delete: nil];
///////////////////////////////////////////////////////////////
如何限制文本框只能输入数字:
建立NSNumberFormatter的子类,增加这个方法,将formatter链接至文本框。
- (BOOL) isPartialStringValid: (NSString **) partialStringPtr
proposedSelectedRange: (NSRangePointer) proposedSelRangePtr
originalString: (NSString *) origString
originalSelectedRange: (NSRange) origSelRange
errorDescription: (NSString **) error
{
NSCharacterSet *nonDigits;
NSRange newStuff;
NSString *newStuffString;
nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
newStuff = NSMakeRange(origSelRange.location,
proposedSelRangePtr->location
- origSelRange.location);
newStuffString = [*partialStringPtr substringWithRange: newStuff];
if ([newStuffString rangeOfCharacterFromSet: nonDigits
options: NSLiteralSearch].location != NSNotFound) {
*error = @"不是数字";
return (NO);
} else {
*error = nil;
return (YES);
}
}
///////////////////////////////////////////////////////////////
从文本框获取十六进制数据的代码
char singleNumberString[3] = {'\0','\0','\0'};
uint32_t singleNumber = 0;
uint32_t i = 0;
NSMutableData *data = [NSMutableData data];
//从文本框获取到得数据
const char *buf = [[_hexToSendTextField text] UTF8String];
//转换为十六进制
for(i = 0; i < strlen(buf); i+=2)
{
if(((i+1) < len && isxdigit(buf) && (isxdigit(buf[i+1])))
{
singleNumberString[0] = buf;
singleNumberString[1] = buf[i+1];
sscanf(singleNumberString, "%x", &singleNumber);
[data appendBytes:(void*)(&tmp) length:1];
}
else
{
break;
}
}
//输出
NSLog(@"%@", data);
/////////////////////////////////////////////////////////////
点击 UITextView 输入文字,光标都从最初点开始
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSRange range;
range.location = 0;
range.length = 0;
textView.selectedRange = range;
}
///////////////////////////////////////////////////////////
软键盘
在登录页面要实现用户名和密码,密码要是点点格式,引入当前页面光标要停留在用户名选项,软键盘要弹出界面。如下图:
弹出键盘:
[username becomeFirstResponder];
取消键盘:
[username resignFirstResponder];
密码保护:
password.secureTextEntry=YES;
//////////////////////////////////////////////////////////////////
1.UITextField的初始化和设置
textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
[textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
textField.placeholder = @"password"; //默认显示的字
textField.secureTextEntry = YES; //密码
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
textField.delegate = self;
2.要实现的Delegate方法,关闭键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.textField resignFirstResponder];
return YES;
}
3. 可以在UITextField使用下面方法,按return键返回
-(IBAction) textFieldDone:(id) sender
{
[textFieldName resignFirstResponder];
}
链接TextField控件的"Did end on exit"
////////////////////////////////////////////////////////////////////
限制输入文本的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location >= MAX_LENGTH)
return NO; // return NO to not change text
return YES;
}
if (textField.text.length >= 10 && range.length == 0)
return NO;
return YES;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([textField.text length] > MAXLENGTH)
{
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
return NO;
}
return YES;
}
//////////////////////////////////////////////////////////////////////
使用UITextFieldDelegate来隐藏键盘
在iPhone界面上,时常会需要当用户输入完内容后,隐藏键盘。 当然有很多方法,今天只介绍使用UITextFieldDelegate这个协议实现隐藏键盘。
其实很简单, 需要三步:
1. 在你的控制器类中,加入UITextFieldDelegate这个协议
如:@interface AddItemViewController : UIViewController <UITextFieldDelegate>
2. 在使用了UITextFieldDelegate协议的控制器类的实现中,加入- (BOOL)textFieldShouldReturn:方法。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
//设置焦点:
[UITextField becomeFirstResponder];
3. 将xib文件中的TextField控件的delegate变量指向到之前使用UITextFieldDelegate协议的那个控制器类,将 TextField的delegate IBOutlet变量右键链接到前面的控制器类的实例上。或者使用代码方式,指定相关TextField的delegate变量。
- (void)viewDidLoad
{
[super viewDidLoad];
itemNameField.delegate = self;
priceField.delegate = self;
}
---------------------------
1.创建并初始化
创建UITextView的文件,并在.h文件中写入如下代码:
#import <UIKit/UIKit.h>
@interface TextViewController : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@end
在.m文件中初始化这个textview,写入代码如下:
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease]; |
self.textView.textColor = [UIColor blackColor]; |
self.textView.font = [UIFont fontWithName: @"Arial" size:18.0]; |
self.textView. delegate = self; |
self.textView.backgroundColor = [UIColor whiteColor]; |
self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country." ; |
self.textView.returnKeyType = UIReturnKeyDefault; |
self.textView.keyboardType = UIKeyboardTypeDefault; |
self.textView.scrollEnabled = YES; |
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight; |
[self.view addSubview: self.textView]; |
2. UITextView退出键盘的几种方式
因为你点击UITextView会出现键盘,如果你退出键盘,有如下几种方式:
(1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。
代码如下:
- ( void )textViewDidBeginEditing:(UITextView *)textView { |
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease]; |
self.navigationItem.rightBarButtonItem = done; |
- ( void )textViewDidEndEditing:(UITextView *)textView { |
self.navigationItem.rightBarButtonItem = nil; |
[self.textView resignFirstResponder]; |
(2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:
#pragma mark - UITextView Delegate Methods |
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text |
if ([text isEqualToString: @"\n" ]) { |
|
[textView resignFirstResponder]; |
|
这样无论你是使用电脑键盘上的回车键还是使用弹出键盘里的return键都可以达到退出键盘的效果。
(3)还有你也可以自定义其他加载键盘上面用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。
代码如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)]; |
[topView setBarStyle:UIBarStyleBlack]; |
|
UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle: @"Hello" style:UIBarButtonItemStyleBordered target:self action:nil]; |
|
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; |
|
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle: @"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)]; |
|
NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil]; |
|
[topView setItems:buttonsArray]; |
|
[tvTextView setInputAccessoryView:topView]; |
|
-(IBAction)dismissKeyBoard |
|
[tvTextView resignFirstResponder]; |
|
(4)设置UITextView圆角问题
做法是在 #import QuartzCore/QuartzCore.h 后,便能調用 [textView.layer setCornerRadius:10]; 來把 UITextView 设定圓角
(5)UITextView根据文本大小自适应高度
通过实现文本字数来确定高度,如下:
NSString * desc = @"Description it is a test font, and don't become angry for which i use to do here.Now here is a very nice party from american or not!" ; |
CGSize size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap]; |
|
然后需要定义UITextView的numberoflines为0,即不做行数的限制。如下:
[textView setNumberOfLines:0]; |
[textView setFrame:CGRectMake(40, 135, 240, size.height+10)]; |
|
(6)UITextView自定选择文字后的菜单
在ViewDidLoad中加入:
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle: @"分享到新浪微博" action:@selector(changeColor:)]; |
UIMenuController *menu = [UIMenuController sharedMenuController]; |
[menu setMenuItems:[NSArray arrayWithObject:menuItem]]; |
当然上面那个@selector里面的changeColor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender |
if (action == @selector(changeColor:)) |
if (textView.selectedRange.length>0) |
}