1.UITextView 也稱作智能標籤。
(1). -[UITextView setContentToHTMLString:] 可以設定html內容,但是這是個私人api的函數。
顯示大段文本首選UITextView,不是UILabel。
(2).通過設定inputAccessoryView,增加一行工具列來關閉鍵盤
{
UIToolbar* inputAccessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[inputAccessoryView setBarStyle:UIBarStyleBlack];
UIBarButtonItem* spaceButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"完成輸入" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)] autorelease];
NSArray * buttonsArray = [NSArray arrayWithObjects:spaceButton,doneButton,nil];
[inputAccessoryView setItems:buttonsArray];
[textView setInputAccessoryView:inputAccessoryView];
}
-(void)dismissKeyBoard
{
[textView resignFirstResponder];
}
(3)最完美的帶底線的多行輸入
http://115.com/file/dpko1yxw#LinedTextView.zip
(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];
更多UITextField 屬性講解
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.當在英文和中文IME之間切換時,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;
(2)同一個UILabel內能顯示多種顏色的文字
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.切換IME
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.