[self.view addSubView:textField];
[textField becomeFirstResponser];
1.UITextView 也称作智能标签。
(1). -[UITextView setContentToHTMLString:]
可以设置html内容,但是这是个私有api的函数。
显示大段文本首选UITextView,不是UILabel。
(2).通过设置inputAccessoryView,增加一行工具栏来关闭键盘
{}
http://115.com/file/dpko1yxw#LinedTextView.zip //对ios7支持不好了
http://code4app.com/ios/Note-View/511358c26803fadc70000000
(4).限制输入长度
static const int KMaxTextLength=280;
#pragma mark -
#pragma mark UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
int length=[textView.text lengthOfBytesUsingEncoding:enc];
int addLength=[text lengthOfBytesUsingEncoding:enc];
if(length>= KMaxTextLength && addLength>0 || length+addLength>KMaxTextLength)
{
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
int length=[textView.text lengthOfBytesUsingEncoding:enc];
iCountLabel.text=[NSString stringWithFormat:@"%d",length];
}
(5).取得UITextView中的光标所在位置
http://blog.csdn.net/liyanbo1984/article/details/5807095
2.UITextField实现了UITextInput协议,UITextInput协议实现了UIKeyInput协议,UIKeyInput协议实现了UITextInputTraits协议,该协议提供了7个属性:文本自动大写样式等。
(1) UITextField 部分属性:
placeholder: 占位符提示;默认使用70%灰色,UITextView没有placeholder属性。
borderStyle: 输入框边框样式;
clearButtonMode:UITextFieldViewModeWhileEditing 在输入框右侧显示圆形叉号,用以清除内容。它会被rightView覆盖。
inputView: 替换掉标准键盘
UIReturnKeyType returnKeyType: // default is UIReturnKeyDefault (See note under UIReturnKeyType enum)
autocapitalizationType=UITextAutocapitalizationTypeNone;//去掉首字母大写,autocapitalizationType默认是UITextAutocapitalizationTypeSentences
contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//文字重直居中
(2) 设置 leftView,rifghtView 示例
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightViewMode = UITextFieldViewModeAlways;
UILabel* label1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
label1.text=@"left";
label1.textAlignment=UITextAlignmentLeft;
textField.leftView=label1;
[label1 release];
label1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
label1.text=@"right";
label1.textAlignment=UITextAlignmentRight;
textField.rightView=label1;
[label1 release];
http://blog.csdn.net/jinglijun/article/details/7058632
http://www.uplook.cn/index-Index-show-view17944.html
http://unmi.cc/uilable-uitextfield-padding-insets
iOS中UITextField 使用全面解析
http://my.oschina.net/u/936286/blog/131010
5. UITextFieldDelegate中
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 用来过滤输入的内容。
这是逐字运行的,每次点击键盘后调用该方法。
可以使用以下句法过滤:
NSString *filtered = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
NSPredicate........
只有在UITextView处在isFirstResponder时,才会触发UITextViewDelegate- (void)textViewDidChange:(UITextView *)textView
6.通用方法解决UITextFiled输入的时候,键盘遮挡问题
http://blog.csdn.net/favormm/article/details/6799942
iPhone竖屏时键盘高216,弹出时间0.3s。
iPad 高度264 352 318 406 54中文行高
ios5动态获取键盘高度 216,252两个高度。UIKeyboardWillChangeFrameNotification通知并不能按照所想的那样发生,要注意以下几点:
a.当在英文和中文输入法之间切换时,iPhone中并不会产生UIKeyboardWillChangeFrameNotification和UIKeyboardDidChangeFrameNotification通知,而iPad中会产生;
b.ChangeFrame Notification会发生在Show或Hide Notification之前;
c.在iPad中分割键盘会有Hide Notification,合并键盘时才有Show Notification。分割时键盘竟然是隐藏的;
参考 http://xcodev.com/wordpress/?p=410
-(void) keyboardDidShow: (NSNotification *)notif
{
NSDictionary* userInfo = [notif userInfo];
NSValue* value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
float keyBoardHeight=[value CGRectValue].size.height;//高216 keyBoardHeight=252;
//value = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//NSTimeInterval animationDuration;
//[value getValue:&animationDuration];
}
9. 在info.plist中,添加Localizations:Chinese,设置长按输入框出现的功能层为中文(拷贝等)。
10.UILabel
(1) //阴影
label.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.75f];
label.shadowOffset = CGSizeMake(0.0f, -2.0f);
另一个笨方法:定义两个UILabel 叠加,底层的UILabel文字为阴影的半透明效果,上层的UILabel和下层的UILabel偏移一个像素并且背景透明,也能起到阴影效果。
//单行,超长省略
label_.lineBreakMode=UILineBreakModeTailTruncation;
label_.numberOfLines=1;
three20
http://github.com/facebook/three20/
FontLabel
http://github.com/zynga/FontLabel/
http://www.cocoachina.com/bbs/simple/?t14541.html
13.自定义placeholder
继承UITextField,重写drawPlaceholderInRect:
- (void)drawPlaceholderInRect:(CGRect)rect
{
// Set colour and font size of placeholder text
[color(0xc4,0xc4,0xc4) setFill];
[[self placeholder] drawInRect:rect withFont:[Utility fontWithSize:12]];
}
16.切换输入法
UITextInputMode类是在4.2及之后才有的一个新类,用来获取当前文本输入模式。即在用户输入文本时,判断使用的是什么键盘。
在改变输入方式时,获得此值,如下:
注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChangeMode:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
-(void) keyboardChangeMode:(NSNotification *)notification
{
NSLog(@"keyboardChangeMode:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
}
en-US //英文
zh-Hans //中文简体
zh-Hant //中文繁体
或者不注册通知,随时使用也可以:NSLog(@"currentInputMode:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
http://blog.sina.com.cn/s/blog_69081e060100v7ad.html
17.使用增加的initWithFrame..开头的类别创建的textField,显示出来的UITextField响应不了键盘事件。所以,要采用静态方法的方式封装构造函数。
18.
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames )
{
printf( "Family: %s \n", [familyName UTF8String] );
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames )
{
printf( "\tFont: %s \n", [fontName UTF8String] );
}
}
http://www.cocoachina.com/bbs/simple/?t70231.html
http://www.cnblogs.com/tracy-e/archive/2010/11/04/1869250.html
19.UITextField 切换密码明文形式
iPasswordTextField.secureTextEntry=YES;
UIButton* eyeButton=[UIButton buttonWithOrigin:CGPointMake(0, 0) normalImage:[UIImage imageNamed:@"eye.png"] hightlightedImage:nil selectedImage:nil];
[eyeButton addTarget:self action:@selector(showOrHidePassword) forControlEvents:UIControlEventTouchUpInside];
iPasswordTextField.rightView=eyeButton;
iPasswordTextField.rightViewMode = UITextFieldViewModeAlways;
iPasswordTextField.clearButtonMode=UITextFieldViewModeNever;
-(void)showOrHidePassword
{
[iPasswordTextField resignFirstResponder];//如果不先失去焦点,设置不成功。
BOOL isSecureTextEntry=iPasswordTextField.isSecureTextEntry;
[iPasswordTextField setSecureTextEntry:!isSecureTextEntry];
}
20.// drawing and positioning overrides
重写下面一些方法,可以实现一些需求
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
21.不能无缘无故强制显示虚拟键盘,可以制造一个假的看不见的UITextField,然后
[self.view addSubView:textField];
[textField becomeFirstResponser];
22.
http://www.cocoachina.com/applenews/devnews/2013/1126/7417.html