ios有没有一些API可以手动显示键盘

发布于 2024-11-28 18:13:30 字数 49 浏览 0 评论 0原文

我有一个自定义视图,我想显示键盘作为我的输入并与其交互。

谢谢大家~

I have a custom view, i want display a keyboard as my input and intercommunicate with it.

thanks guys~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2024-12-05 18:13:30

在您的自定义视图中,您必须添加一个零大小的 UITextField,然后将您的视图设置为其委托。然后在自定义视图中定义手势识别器(例如单击),并在手势识别器识别事件中将文本字段设置为第一响应者。
一旦您点击视图,键盘就会弹出。然后编写委托方法:


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

管理与类型化字符串的交互。如果您需要更新用户键盘输入的视图,请不要忘记调用 setNeedsDisplay。

下面的例子有效。当然,您必须在视图控制器中实例化自定义视图。在示例中,只需在键盘中键入一个字母,它就会显示在视图上。



//
//  MyView.h
//

#import 

@interface MyView : UIView {

    UITextField *tf;

}

@property (nonatomic,copy) NSString *typed;

@end

//
//  MyView.m
//

#import "MyView.h"

@implementation MyView

@synthesize typed;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor=[UIColor redColor];
        tf = [[UITextField alloc] initWithFrame:CGRectZero];
        tf.delegate=self;
        [self addSubview:tf];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect
{
    // Draw the "typed" string in the view
    [[UIColor blackColor] setStroke];
    if(self.typed) {
        [self.typed drawAtPoint:CGPointZero withFont:[UIFont systemFontOfSize:50]];
    }
}


-(void)tap:(UIGestureRecognizer *)rec {
    if(rec.state==UIGestureRecognizerStateEnded) {
        [tf becomeFirstResponder];
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    self.typed=[string substringToIndex:1]; // extract the first character of the string
    [self setNeedsDisplay]; // force view redraw
}

@end

In your custom view you must add a zero-sized UITextField and then set your view as its delegate. Then in your custom view define a gesture recognizer (e.g. a single tap) and in the gesture recognizer recognition event set the text field as first responder.
As soon as you tap the view the keyboard will popup. Then write the delegate method:


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

to manage the interaction with the typed string. Don't forget to call setNeedsDisplay if you need to update your view on the user keyboard typing.

The example below works. You must instantiate the custom view in your view controller of course. With the example, just type a letter in the keyboard and it will be displayed on the view.



//
//  MyView.h
//

#import 

@interface MyView : UIView {

    UITextField *tf;

}

@property (nonatomic,copy) NSString *typed;

@end

//
//  MyView.m
//

#import "MyView.h"

@implementation MyView

@synthesize typed;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor=[UIColor redColor];
        tf = [[UITextField alloc] initWithFrame:CGRectZero];
        tf.delegate=self;
        [self addSubview:tf];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect
{
    // Draw the "typed" string in the view
    [[UIColor blackColor] setStroke];
    if(self.typed) {
        [self.typed drawAtPoint:CGPointZero withFont:[UIFont systemFontOfSize:50]];
    }
}


-(void)tap:(UIGestureRecognizer *)rec {
    if(rec.state==UIGestureRecognizerStateEnded) {
        [tf becomeFirstResponder];
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    self.typed=[string substringToIndex:1]; // extract the first character of the string
    [self setNeedsDisplay]; // force view redraw
}

@end

早乙女 2024-12-05 18:13:30

我认为解决方案可以像在类中实现 UIKeyInput 协议一样简单。

阅读有关 UIKeyInput 协议参考

I think the solution could be as simple as implementing the UIKeyInput protocol in your class.

Read more about the UIKeyInput Protocol Reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文