ios Input Views and Input Accessory Views

文档说明:

Custom Views for Data Input

UIKit allows applications to substitute custom input views for the system keyboard. It also enables applications to have an accessory view above the system keyboard or custom input view. Additionally, it enables applications to play key-click sounds when users tap on a controls of an input view or input accessory view.

Input Views and Input Accessory Views

The UIKit framework includes support for custom inputviews and input accessory views. Your application can substitute its own input view for the system keyboard when users edit text or other forms of data in a view. For example, an application could use a custom input view to enter characters from a runic alphabet. You may also attach an input accessory view to the system keyboard or to a custom input view; this accessory view runs along the top of the main input view and can contain, for example,controls that affect the text in some way or labels that display some information about the text.

To get this feature if your application is using UITextView and UITextField objects for text editing, simply assign custom views to theinputView and inputAccessoryView properties. Those custom views are shown when the text object becomesfirst responder.

You are not limited to input views and input accessory views in framework-supplied text objects. Any class inheriting directly or indirectly fromUIResponder (usually a custom view) can specify its own input view and input accessory view. TheUIResponder class declares two properties for input views and input accessory views:


根据官方例子写了一个一样的demo


新建一个工程名为KeyBoardAddTool

在工程里添加一个view  xib  名称为 AccessoryView

如图 

 



在ViewContorller的视图里拖拽个Textview

代码如下

//  ViewController.h

//  KeyBoardAddTool

//

//  Created by lengshengren on 13-9-10.

//  Copyright (c) 2013 lengshengren. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController

{

    UITextView *textView;

    UIView *accessoryView;

    

}

@property (strong, nonatomic) IBOutlet UITextView *textView;


@property (strong, nonatomic) IBOutlet UIView *accessoryView;



- (IBAction)sendMessage:(id)sender;




@end


//

//  ViewController.m

//  KeyBoardAddTool

//

//  Created by lengshengren on 13-9-10.

//  Copyright (c) 2013 lengshengren. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize textView;

@synthesize accessoryView;

- (void)viewWillAppear:(BOOL)animated

{

    

     /*开启光标弹起键盘*/

    

    [super viewWillAppear:animated];

    [textViewbecomeFirstResponder];

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    /*键盘监听*/

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];


// Do any additional setup after loading the view, typically from a nib.

}


- (void)viewDidUnload {

    [superviewDidUnload];

    

 

    

    self.textView =nil;

    self.accessoryView =nil;

    

    /* 删除监听*/

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];

}


#pragma mark -

#pragma mark Text view delegate methods


- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {

    

    /*

         添加AccessoryView 附属视图

     */

    

    if (textView.inputAccessoryView ==nil) {

        [[NSBundle mainBundle] loadNibNamed:@"AccessoryView"owner:selfoptions:nil];

        // Loading the AccessoryView nib file sets the accessoryView outlet.

        textView.inputAccessoryView =accessoryView;

        // After setting the accessory view for the text view, we no longer need a reference to the accessory view.

        

    // 设置完之后我们不需要总是对附属视图进行引用!可以置为空

        self.accessoryView =nil;

    }

    

    return YES;

}


- (BOOL)textViewShouldEndEditing:(UITextView *)aTextView {

    [aTextView resignFirstResponder];

    return YES;

}


#pragma mark -

#pragma mark Responding to keyboard events


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

    

    /*

        键盘在中文和英文的情况下高度不通!下面判端键盘的高度

     */

    

    NSDictionary *userInfo = [notification userInfo];

    

    // Get the origin of the keyboard when it's displayed.

    NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];

    

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.

    CGRect keyboardRect = [aValue CGRectValue];

    keyboardRect = [self.viewconvertRect:keyboardRect fromView:nil];

    

    CGFloat keyboardTop = keyboardRect.origin.y;

    CGRect newTextViewFrame = self.view.bounds;

    newTextViewFrame.size.height = keyboardTop -self.view.bounds.origin.y;

    

    // Get the duration of the animation.

    NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.

    [UIView beginAnimations:nilcontext:NULL];

    [UIView setAnimationDuration:animationDuration];

    

    textView.frame = newTextViewFrame;

    

    [UIView commitAnimations];

}



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

    

    NSDictionary* userInfo = [notification userInfo];

    

    /*

     Restore the size of the text view (fill self's view).

     Animate the resize so that it's in sync with the disappearance of the keyboard.

     */

    NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    

    [UIView beginAnimations:nilcontext:NULL];

    [UIView setAnimationDuration:animationDuration];

    

    textView.frame =self.view.bounds;

    

    [UIView commitAnimations];

}




- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)sendMessage:(id)sender {

    // When the accessory view button is tapped, add a suitable string to the text view.

    NSMutableString *text = [textView.textmutableCopy];

    NSRange selectedRange = textView.selectedRange;

    

    [text replaceCharactersInRange:selectedRangewithString:@"发送消息"];

    textView.text = text;

 

}


- (void)dealloc {

    

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:nil object:nil];



}

效果如图



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值