//設(shè)置占位符
text.placeholder=@"Placeholder";
//設(shè)置邊框格式
text.borderStyle=UITextBorderStyleRoundedRect;
//設(shè)置文字水平位置
text.textAlignment=NSTextAlignmentLeft;
//設(shè)置文字垂直位置
text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
當(dāng)彈出的鍵盤(pán)遮住了文本框時(shí),可用以下方法來(lái)設(shè)置適應(yīng)高度
先注冊(cè)2個(gè)通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWasShown:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardDidHide:)name:UIKeyboardWillHideNotificationobject:nil];
好像UIKeyboardWillShowNotification,UIKeyboardWillHideNotification看上去鍵盤(pán)和文本框移動(dòng)位置上比較順暢,
實(shí)現(xiàn)2個(gè)通知
-(void)keyboardWasShown:(NSNotification*)notification
{
NSDictionary*info=[notification userInfo];
CGSizesize=[[infoobjectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
[UIView beginAnimations:@"skip"context:nil];
[UIView setAnimationDuration:0.1];
CGFloatall=[UIScreenmainScreen].bounds.size.height;
CGFloatoykey=all-size.height;
CGFloatoffset=self.textview.frame.origin.y+self.textview.frame.size.height-oykey;
if(offset>0)
{
self.view.frame=CGRectMake(self.view.frame.origin.x,-offset-10,self.view.frame.size.width,self.view.frame.size.height+offset+10);
UITextView
自適應(yīng)高度
試了好幾種方法:
一:sizeWithFont:constrainedToSize:lineBreakMode:
CGRectorgRect=self.textView.frame;//獲取原始UITextView的frame
CGSizesize = [self.aModel.zdescription sizeWithFont:[UIFontsystemFontOfSize:12] constrainedToSize:CGSizeMake(280, FLT_MAX)lineBreakMode:NSLineBreakByCharWrapping];
這個(gè)方法對(duì)于有些能完全顯示,有些會(huì)多出一段空白,有些就會(huì)截掉一些文字。
不過(guò)對(duì)于UIlabel 是可以用得。
二:contentSize.height
這個(gè)在6之前有用,7就不適用了
orgRect.size.height=self.textView.contentSize.height;//獲取自適應(yīng)文本內(nèi)容高度
三:sizeThatFits
能解決自適應(yīng)高度問(wèn)題
CGSize myTextViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
self.myTextView.height = myTextViewSize.height;
NSLog(@"%f", self.myTextView.height);
獲取硬件的輸入鍵盤(pán)
- (UIView*)getKeyboardView{
UIView*result =nil;
NSArray*windowsArray = [UIApplicationsharedApplication].windows;
for(UIView*tmpWindowinwindowsArray) {
NSArray*viewArray = [tmpWindowsubviews];
for(UIView*tmpViewinviewArray) {
if([[NSStringstringWithUTF8String:object_getClassName(tmpView)]isEqualToString:@"UIInputSetContainerView"]) {
result = tmpView;
break;
}
}
if(result !=nil) {
break;
}
}
returnresult;
}
如果要做自定義鍵盤(pán),首先獲取系統(tǒng)鍵盤(pán),然后addsubview 將其覆蓋,實(shí)現(xiàn)自定義鍵盤(pán)