Uitextfield uitextView自适应高度无标题文
//设置占位符
text.placeholder=@"Placeholder";
//设置边框格式
text.borderStyle=UITextBorderStyleRoundedRect;
//设置文字水平位置
text.textAlignment=NSTextAlignmentLeft;
//设置文字垂直位置
text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
当弹出的键盘遮住了文本框时,可用以下方法来设置适应高度
先注册2个通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWasShown:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardDidHide:)name:UIKeyboardWillHideNotificationobject:nil];
好像UIKeyboardWillShowNotification,UIKeyboardWillHideNotification看上去键盘和文本框移动位置上比较顺畅,
实现2个通知
-(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
自适应高度
试了好几种方法:
一:sizeWithFont:constrainedToSize:lineBreakMode:
CGRectorgRect=self.textView.frame;//获取原始UITextView的frame
CGSizesize = [self.aModel.zdescription sizeWithFont:[UIFontsystemFontOfSize:12] constrainedToSize:CGSizeMake(280, FLT_MAX)lineBreakMode:NSLineBreakByCharWrapping];
这个方法对于有些能完全显示,有些会多出一段空白,有些就会截掉一些文字。
不过对于UIlabel 是可以用得。
二:contentSize.height
这个在6之前有用,7就不适用了
orgRect.size.height=self.textView.contentSize.height;//获取自适应文本内容高度
三:sizeThatFits
能解决自适应高度问题
CGSize myTextViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
self.myTextView.height = myTextViewSize.height;
NSLog(@"%f", self.myTextView.height);
获取硬件的输入键盘
- (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;
}
如果要做自定义键盘,首先获取系统键盘,然后addsubview 将其覆盖,实现自定义键盘