二种方式遮盖键盘

by admin on 2019年9月14日

率先设置textfiled的delegate为self

一:编辑被键盘遮挡的标题    (推荐应用第三方库:IQKeyboardManager  用法参考)

 参考自:

一:编辑被键盘遮挡的题目    (推荐使用第三方库:IQKeyboardManager  用法参谋)

 参考自:

– (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text{

然后达成textFieldShouldReturn那些代理

1.自定方法 ,用于移动视图

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight
withDuration:(NSTimeInterval)_NSTimeInterval;

1.自定方法 ,用于移动视图

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight
withDuration:(NSTimeInterval)_NSTimeInterval;

if ([text isEqualToString:@”\n”]) {

textFieldShouldReturn会在会用点击return的时候调用,return的值表示系统是不是要选用暗中认可的一颦一笑。

2.注册监听

NSNotificationCenter *defaultCenter = [NSNotificationCenter
defaultCenter];

    [defaultCenter selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];

    [defaultCenter addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];

2.报了名监听

NSNotificationCenter *defaultCenter = [NSNotificationCenter
defaultCenter];

    [defaultCenter selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];

    [defaultCenter addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];

[textview resignFirstResponder];

– textFieldShouldReturn:(UITextField *)textField {

3.兑现格局

– (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

    NSValue* aValue = [userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSValue *animationDurationValue = [userInfo
objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    if(nil==self.myTextView) return;//    self.editTextView
为被键盘遮挡住的控件

    CGRect rect = self.myTextView.frame;

    float textY = rect.origin.y + rect.size.height; 

    float bottomY = SCREENHEIGHT – textY;//获得上面框到后面部分的偏离
 SCREENHEIGHT 为当下设施的万丈

    if(bottomY >=keyboardRect.size.height
){//键盘暗中同意中度,假若超越其中度,则直接重临

        return;

    }

    float moveY = keyboardRect.size.height – bottomY;

    [self moveInputBarWithKeyboardHeight:moveY
withDuration:animationDuration];

}

 

– (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo
objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [self moveInputBarWithKeyboardHeight:0.0
withDuration:animationDuration];

二种方式遮盖键盘。}

 

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight
withDuration:(NSTimeInterval)_NSTimeInterval{

  CGRect rect1 = self.view.frame;

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:_NSTimeInterval];

    rect1.origin.y = -_CGRectHeight;//view往上活动

    self.view.frame = rect1;

    [UIView commitAnimations];

}

3.达成方式

– (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

    NSValue* aValue = [userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSValue *animationDurationValue = [userInfo
objectForKey:UIKeyboardAnimationDurationUserInfoKey]二种方式遮盖键盘。;

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    if(nil==self.myTextView) return;//    self.editTextView
为被键盘遮挡住的控件

    CGRect rect = self.myTextView.frame;

    float textY = rect.origin.y + rect.size.height; 

    float bottomY = SCREENHEIGHT – textY;//获得下面框到底部的离开
 SCREENHEIGHT 为当前器具的可观

    if(bottomY >=keyboardRect.size.height
){//键盘默许中度,倘使高出个中度,则从来回到

        return;

    }

    float moveY = keyboardRect.size.height – bottomY;

    [self moveInputBarWithKeyboardHeight:moveY
withDuration:animationDuration];

}

 

– (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo
objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [self moveInputBarWithKeyboardHeight:0.0
withDuration:animationDuration];

}

 

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight
withDuration:(NSTimeInterval)_NSTimeInterval{

  CGRect rect1 = self.view.frame;

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:_NSTimeInterval];

    rect1.origin.y = -_CGRectHeight;//view往上活动

    self.view.frame = rect1;

    [UIView commitAnimations];

}

return NO;

[textField resignFirstResponder];

二: 键盘

二: 键盘

}

return NO;

(1)键盘类型 

   
 UIKeyboardTypeDefault, // 暗中认可键盘:援助具备字符 

  1.  UIKeyboardTypeASCIICapable, // 扶助ASCII的默许键盘 
  2.  UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,帮助+*#等符号 
  3.  UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符 
  4.  UIKeyboardTypeNumberPad,  //纯多功能键盘 (不带小数点)
  5.  UIKeyboardTypeDecimalPad  //超薄键盘 (带小数点)
  6.  UIKeyboardTypePhonePad,   // 电话键盘 
  7.  UIKeyboardTypeNamePhonePad, // 电话键盘,也支撑输入人名字 
  8.  UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘
  9.  UIKeyboardTypeWebSearch     //用于搜索
  10. UIKeyboardTypeAlphabet

   如:  self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

   祥见:

(1)键盘类型 

   
 UIKeyboardTypeDefault, // 暗许键盘:帮助具有字符 

  1.  UIKeyboardTypeASCIICapable, // 帮助ASCII的默许键盘 
  2.  UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,匡助+*#等符号 
  3.  UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符 
  4.  UIKeyboardTypeNumberPad,  //纯超薄键盘 (不带小数点)
  5.  UIKeyboardTypeDecimalPad  //机械键盘 (带小数点)
  6.  UIKeyboardTypePhonePad,   // 电话键盘 
  7.  UIKeyboardTypeNamePhonePad, // 电话键盘,也支撑输入人名字 
  8.  UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘
  9.  UIKeyboardTypeWebSearch     //用于寻觅
  10. UIKeyboardTypeAlphabet

   如:  self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

   祥见:

return YES;

}

(2) return键的体系 

       UIReturnKeyDefault, 暗中认可 米黄按钮,标有Return 

     UIReturnKeyGo,     标有Go的青莲按键         
 (完成,可用来填写资料的尾声一项)

     UIReturnKey谷歌(Google),标有谷歌的雪青开关,用语搜索 

     UIReturnKeyJoin,标有Join的花青按键 

     UIReturnKeyNext,标有Next的海洋蓝开关           
 (可用于登入/注册/填写地址–>下一步)

     UIReturnKeyRoute,标有Route的雪白开关 

     UIReturnKeySearch,标有Search的赤褐按键     (可用以寻找)

     UIReturnKeySend,标有Send的天灰开关          

     UIReturnKeyYahoo,标有Yahoo的本白开关 

     UIReturnKeyYahoo,标有Yahoo的青灰开关 

     UIReturnKeyEmergencyCall, 急迫呼唤开关

  如:

 self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

(2) return键的档案的次序 

       UIReturnKeyDefault, 暗许 黄绿按键,标有Return 

     UIReturnKeyGo,     标有Go的铁锈红开关         
 (完毕,可用于填写资料的终极一项)

     UIReturnKey谷歌(Google),标有Google的深草绿按键,用语寻觅 

     UIReturnKeyJoin,标有Join的鲜蓝开关 

     UIReturnKeyNext,标有Next的白色按键           
 (可用于登陆/注册/填写地址–>下一步)

     UIReturnKeyRoute,标有Route的中绿按键 

     UIReturnKeySearch,标有Search的石磨蓝按钮     (可用来找出)

     UIReturnKeySend,标有Send的蓝紫按键          

     UIReturnKeyYahoo,标有Yahoo的宝石红开关 

     UIReturnKeyYahoo,标有Yahoo的黄绿开关 

     UIReturnKeyEmergencyCall, 急切呼叫按键

  如:

 self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

}

第一设置textview的delegate为self,然后达成textView
shouldChangeTextInRange这个代理

(3) 点击return建响应事件

   A.UITextField –> – (BOOL)textFieldShouldReturn:(UITextField
*)textField{

   如:  增多地址

– (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if(textField.tag==10)  //下一步 (姓名)

    {

        [self.uName resignFirstResponder];

        [self.uIphone becomeFirstResponder];

    }else if(textField.tag==11)   //下一步 (电话)

    {

        [self.uIphone resignFirstResponder];

        [self.reciveAddress becomeFirstResponder];

    }else if(textField.tag==100)   //完结(地址填完之后可径直调用接口)

    {

        [self.reciveAddress resignFirstResponder];

         [self addBtn:nil];     

    }

    return YES;

}

B.UITextView –> – (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

如:

– (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text{
    if ([text isEqualToString:@”\n”]){
//判定输入的字是还是不是是回车,即按下return
        //在此处做你响应return键的代码

         [self addBtn:nil];

        return NO;
//这里再次回到NO,就象征return键值失效,即页面上按下return,不会冒出换行,假如为yes,则输入页面会换行
    }

    return YES;
}

参考自:

 

(3) 点击return建响应事件

   A.UITextField –> – (BOOL)textFieldShouldReturn:(UITextField
*)textField{

   如:  增添地址

– (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if(textField.tag==10)  //下一步 (姓名)

    {

        [self.uName resignFirstResponder];

        [self.uIphone becomeFirstResponder];

    }else if(textField.tag==11)   //下一步 (电话)

    {

        [self.uIphone resignFirstResponder];

        [self.reciveAddress becomeFirstResponder];

    }else if(textField.tag==100)   //达成(地址填完事后可一直调用接口)

    {

        [self.reciveAddress resignFirstResponder];

         [bf88必发唯一官网 ,self addBtn:nil];     

    }

    return YES;

}

B.UITextView –> – (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

如:

– (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text{
    if ([text isEqualToString:@”\n”]){
//剖断输入的字是或不是是回车,即按下return
        //在那边做你响应return键的代码

         [self addBtn:nil];

        return NO;
//这里重临NO,就意味着return键值失效,即页面上按下return,不会冒出换行,假使为yes,则输入页面会换行
    }

    return YES;
}

参考自:

 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

– textView:(UITextView *)textView shouldChangeTextInRange:range
replacementText:(NSString *)text {

三:UITextField小结

三:UITextField小结

[textview resignFirstResponder];

if([text isEqualToString:@”\n”]) {

   1.自定义UIText菲尔德 左侧加Logo(如:登陆)

    UIImageView *i=[[UIImageView alloc]initWithFrame:CGRectMake(15,
10, 21, 21)];

    [i setImage:[UIImage imageNamed:@”yh”]];

    UIView *userLeftView = [[UIView
alloc]initWithFrame:CGRectMake(0, 0, 40, 38)];

    [userLeftView addSubview:i]; 

    self.userName.leftView=userLeftView;

    self.userName.leftViewMode=UITextFieldViewModeAlways;

     若只是单独的UITextField 缩进:
只需加
 self.userName.leftViewMode=UITextFieldViewModeAlways; self.userName.leftView=[[UIView
alloc]init] 两句就能够。

   1.自定义UITextField 左边加Logo(如:登入)

    UIImageView *i=[[UIImageView alloc]initWithFrame:CGRectMake(15,
10, 21, 21)];

    [i setImage:[UIImage imageNamed:@”yh”]];

    UIView *userLeftView = [[UIView
alloc]initWithFrame:CGRectMake(0, 0, 40, 38)];

    [userLeftView addSubview:i]; 

    self.userName.leftView=userLeftView;

    self.userName.leftViewMode=UITextFieldViewModeAlways;

     若只是独自的UITextField 缩进:
只需加
 self.userName.leftViewMode=UITextFieldViewModeAlways; self.userName.leftView=[[UIView
alloc]init] 两句就能够。

}

[textView resignFirstResponder];

   2.设置自定义UIText菲尔德 的Placeholder颜色   

   (1)方法一:

       UIColor *color = [UIColor blue];

    self.userName.attributedPlaceholder = [[NSAttributedString alloc]
initWithString:@”  手机号”  attributes:@{NSForegroundColorAttributeName:
color}];

(2)方法二:        

   [textField setValue:[UIColor redColor]
forKeyPath:@”_placeholderLabel.textColor”]; 

   [textField setValue:[UIFont boldSystemFontOfSize:16]
forKeyPath:@”_placeholderLabel.font”];

   2.设置自定义UITextField 的Placeholder颜色   

   (1)方法一:

       UIColor *color = [UIColor blue];

    self.userName.attributedPlaceholder = [[NSAttributedString alloc]
initWithString:@”  手机号”  attributes:@{NSForegroundColorAttributeName:
color}];

(2)方法二:        

   [textField setValue:[UIColor redColor]
forKeyPath:@”_placeholderLabel.textColor”]; 

   [textField setValue:[UIFont boldSystemFontOfSize:16]
forKeyPath:@”_placeholderLabel.font”];

-(void)viewWillAppear:(BOOL)animated{

return NO;

   3.合併收起键盘      

    [[[UIApplication sharedApplication] keyWindow]
endEditing:YES];

   3.集结收起键盘      

    [[[UIApplication sharedApplication] keyWindow]
endEditing:YES];

//注册布告,监听键盘弹出事件

}

    4. textFiled  自动换行           

          

    4. textFiled  自动换行           

          

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification
object:nil];

return YES;

四:UITextView小结

四:UITextView小结

//注册通告,监听键盘消失事件

}

  1.设置Placeholder      

    @property (nonatomic,strong) UILabel  *proText1;        

    self.automaticallyAdjustsScrollViewInsets = NO;    

    [self.leaveMessage setDelegate:self];

    UILabel *lbl=[[UILabel
alloc]initWithFrame:CGRectMake(self.leaveMessage.frame.origin.x+10,
self.leaveMessage.frame.origin.y-35,
self.leaveMessage.bounds.size.width, 100)];

    lbl.text=@” 多谢您留下宝贵的意见….”;

    [lbl setFont:[UIFont systemFontOfSize:15.0]];

    lbl.enabled=NO;

    self.proText1=lbl;

    [self.view addSubview:lbl];    

  #pragma mark —–textView的代办事件

  -(void)textViewDidChange:(UITextView *)textView

  {

   //  textView.text = [textView.text
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];

        if (textView.text.length == 0) {

            self.proText1.text = @” 感激您留下宝贵的意见….”;

        }else{

            self.proText1.text = @””;

        }

  }

  1.设置Placeholder      

    @property (nonatomic,strong) UILabel  *proText1;        

    self.automaticallyAdjustsScrollViewInsets = NO;    

    [self.leaveMessage setDelegate:self];

    UILabel *lbl=[[UILabel
alloc]initWithFrame:CGRectMake(self.leaveMessage.frame.origin.x+10,
self.leaveMessage.frame.origin.y-35,
self.leaveMessage.bounds.size.width, 100)];

    lbl.text=@” 多谢您留下宝贵的意见….”;

    [lbl setFont:[UIFont systemFontOfSize:15.0]];

    lbl.enabled=NO;

    self.proText1=lbl;

    [self.view addSubview:lbl];    

  #pragma mark —–textView的代办事件

  -(void)textViewDidChange:(UITextView *)textView

  {

   //  textView.text = [textView.text
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];

        if (textView.text.length == 0) {

            self.proText1.text = @” 多谢您留下宝贵的意见….”;

        }else{

            self.proText1.text = @””;

        }

  }

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification
object:nil];

以此代理会在客户输入大概去除文字时调用,textfiled也可能有其一代理,也足以因此这种措施完结textfiled收回键盘

2.去掉空格以及换行

 NSString *content = [textView.text
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];

部分时候由于不精确的操作,直接将textView.text作为参数传到服务器,只怕会时有产生意料之外的荒唐,由此就需求加这一个对字符串进行一下拍卖

 

 更加多请参见:

 

补充:   

1.长按复制功用

 – (void)viewDidLoad { 

     [self.view addGestureRecognizer:[[UILongPressGestureRecognizer
alloc] initWithTarget:self action:@selector(pasteBoard:)]]; 

}

 – (void)pasteBoard:(UILongPressGestureRecognizer *)longPress { 

          if (longPress.state == UIGestureRecognizerStateBegan) {

                             UIPasteboard *pasteboard = [UIPasteboard
generalPasteboard]; 

                              pasteboard.string = @”须要复制的文书”;

          } }

 

2.导入自定义字体库

     1、找到您想用的字体的 ttf 格式,拖入工程

     2、在工程的plist中追加一行数组,“Fonts provided by application”

     3、为这几个key增添三个item,value为您刚刚导入的ttf文件名

     4、直接选取就可以:label.font = [UIFont
fontWithName:@”你刚刚导入的ttf文件名” size:20.0];

 附:总括文字的size

/**
 *  计算一段文字size
 */
- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);

    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

}
/**
 *  计算一行文字size
 */
- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithFont:font maxW:MAXFLOAT];
}

 

2.去掉空格以及换行

 NSString *content = [textView.text
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];

有时由于不正确的操作,直接将textView.text作为参数传到服务器,大概会发生意料之外的谬误,因而就必要加那一个对字符串进行一下甩卖

 

 更加的多请参见:

 

补充:   

1.长按复制作而成效

 – (void)viewDidLoad { 

     [self.view addGestureRecognizer:[[UILongPressGestureRecognizer
alloc] initWithTarget:self action:@selector(pasteBoard:)]]; 

}

 – (void)pasteBoard:(UILongPressGestureRecognizer *)longPress { 

          if (longPress.state == UIGestureRecognizerStateBegan) {

                             UIPasteboard *pasteboard = [UIPasteboard
generalPasteboard]; 

                              pasteboard.string = @”须要复制的文书”;

          } }

 

2.导入自定义字体库

     1、找到你想用的书体的 ttf 格式,拖入工程

     2、在工程的plist中追加一行数组,“Fonts provided by application”

     3、为这些key增多三个item,value为您刚刚导入的ttf文件名

     4、直接选取就可以:label.font = [UIFont
fontWithName:@”你刚刚导入的ttf文件名” size:20.0];

 附:计算文字的size

/**
 *  计算一段文字size
 */
- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);

    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;

}
/**
 *  计算一行文字size
 */
- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithFont:font maxW:MAXFLOAT];
}

 

}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(KeyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];

// 键盘弹出时

-KeyboardWillShow:(NSNotification *)notification

-(void)keyboardDidShow:(NSNotification *)notification

{

{

NSDictionary *info = [notification userInfo];

//获取键盘中度

//获取中度

NSValue *keyboardObject = [[notification userInfo]
objectForKey:UIKeyboardFrameEndUserInfoKey];

NSValue *value = [info objectForKey:@”UIKeyboardBoundsUserInfoKey”];

CGRect keyboardRect;

CGSize keyboardSize = [value CGRectValue].size;

[keyboardObject getValue:&keyboardRect];

float keyboardHeight = keyboardSize.height;

//调节放置有textView的view的地点

// 获取键盘弹出的日子

//设置动画

NSValue *animationDurationValue = [[notification userInfo]
objectForKey:UIKeyboardAnimationDurationUserInfoKey];

[UIView beginAnimations:nil context:nil];

NSTimeInterval animationDuration;

//定义动画时间

[animationDurationValue getValue:&animationDuration];

[UIView setAnimationDuration:5];

//自定义的frame大小的改观的言辞

//设置view的frame,往上移动

…………….

[(UIView *)[self.view  viewWithTag:1000] setFrame:CGRectMake(0,
self.view.frame.size.height-keyboardRect.size.height-50, 320, 50)];

}

[UIView commitAnimations];

}

//键盘消失时

-(void)keyboardDidHidden

{

//定义动画

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:5];

//设置view的frame,往下平移

[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0,
self.view.frame.size.height-50, 320, 50)];

[UIView commitAnimations];

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图