ios 定制键盘

[转]定制的iOS键盘

我们的应用程序需要输入默认的iOS键盘往往不是最适合提供我们想要的数据排序。当我们发现,我们真的希望有一些额外的控制键盘或想帮助我们的用户输入一组特定的符号,它是时间来定制我们的应用程序的键盘。

控制键盘呢?

我们第一次接触到不同类型的键盘可能来自  UITextField的UITextView的两者都提供符合的  UITextInputTraits  协议,这为我们提供了选项设置键盘类型,返回键类型,键盘的外观,和其他行为。这些选项涵盖了许多类型的文字输入,我们可能要支持纯文本,密码,电子邮件地址,网址,等等。这是一个很好的地方,开始定制键盘选项但  UITextInputTraits  本身的不存在键盘,所以,当我们需要行为协议不提供,我们将不得不继续寻找。

向上遍历类层次结构,我们看到  的UITextField,  UITextView的,实际上所有  的UIView  对象从UIResponer继承  响应对象在我们的观点形成了一个响应链,将有机会作出回应到非触摸输入事件的对象序列。(如果这不是一个熟悉的话题,那么来看看“响应对象和响应链”,“事件处理指南”的iOS系统是如何工作的全面讨论。)每当一个响应成为第一个响应(看到  becomeFirstResponer)它决定什么,如果有的话,键盘需要被显示。UIResponder的  为我们提供了2只读的属性用于控制键盘的外观;  inputView提供的键盘本身和 inputAccessoryView  控制连接到顶部的配件鉴于键盘(编辑表格时,在移动Safari浏览器,例如含有“下一步”和“上一页”按钮)。从这些属性返回自己的意见,我们可以替换系统键盘, 我们关心的任何自定义  的UIView构建。

从简单的开始:自定义的UITextView

UITextField的UITextView的重新定义他们的  inputView  和  inputAccessoryView  属性, 而不是  只读READWRITE当使用这些视图类,因此我们可以设置自己的键盘很容易从一些其他类一样,我们的视图控制器。

假设我们要 在我们的应用程序,支持格式的文本编辑  降价系统键盘很好的工作,但它需要输入#或* \不出现键盘上的三个水龙头。这将会使我们的用户很难强调文本或插入代码块。让我们添加的输入附件给他们“强调”,“强”,“代码”格式控制。

给定一个简单的视图控制器  的UITextView * TextView的UIView的* accessoryView  网点我们可以设置文本视图的 inputAccessoryView的,  我们  viewDidLoad中


1

输入响应

显示配饰视图是唯一的解决方案的一半。我们也希望我们的文本视图的内容进行更改,当用户点击附件视图的按钮之一。让我们创建一个自定义的视图类配饰视图,并给它一个参考的文本字段。

@interface MarkdownInputAccessoryView : UIView

 

@property(nonatomic, weak) id <UITextInput> delegate;

 

- (IBAction)toggleStrong:(id)sender;

- (IBAction)toggleEmphasis:(id)sender;

- (IBAction)toggleCode:(id)sender;

 

@end

 

@implementation MarkdownInputAccessoryView

 

@synthesize delegate;

 

- (IBAction)toggleStrong:(id)sender {

    UITextRange *selectedText = [delegate selectedTextRange];

    if (selectedText == nil) {

        //no selection or insertion point

        //...

    }

    else if (selectedText.empty) {

        //inserting text at an insertion point

        [delegate replaceRange:selectedText withText:@"*"];

        //...

    }

    else {

        //updated a selected range

        //...

    }

}

 

- (IBAction)toggleEmphasis:(id)sender {

    //...

}

 

- (IBAction)toggleCode:(id)sender {

    //...

}

 

@end


然后,我们的控制器可以设定委托财产和附件视图能够更新的文本字段。

//...

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

//...

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.accessoryView.delegate =self.textView;

    self.textView.inputAccessoryView =self.accessoryView;

}


添加自定义UIView的复杂性:键盘

添加自定义的输入视图是添加自定义的输入附件观点大致相同。当我们用的UITextField  或  UITextView的工作,   我们可以创建自定义的视图,并把它传递给相应的属性setter.If但是我们已经建立了一个自定义  UIView的  子类,然后我们需要做的更多一点的工作。

如果它仍然是有意义提供鉴于其输入视图控制器或其他类意见,那么我们就可以重新声明  inputView  和  inputAccessoryView的读写 性能,暴露setter方法,使该视图可以给出参考意见,它应该使用输入。另外,我们可以覆盖inputView  和  inputAccessoryView的 getter方法返回适当的意见。我倾向于选择后者选项,因为它允许一个视图应提交其自己的输入控件如何,但如果选择适当的输入视图依赖对当前接口成语或语言等因素,那么它可能会更有意义的外部服务提供一个输入视图为当前视图。

如果我们想建立一个视图,用于显示比分的乐谱,我们可能会建立类似下面的东西。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

@class MusicScoreView;

 

@interface MusicScoreInputView : UIView

 

@property (nonatomic, weak)IBOutlet MusicScoreView *delegate;

 

@end

 

@interface MusicScoreView ()

 

@property(nonatomic,readwrite, strong)IBOutlet UIView *inputView;

 

- (void) loadInputView;

 

@end

 

@implementation MusicScoreView

 

@synthesize inputView;

 

- (id)initWithFrame:(CGRect)frame {

    self = [super init];

    if (self) {

        [self loadInputView];

    }

    return self;    

}

 

- (id)initWithCoder:(NSCoder *)coder

{

    self = [super initWithCoder:coder];

    if (self) {

        [self loadInputView];

    }

    returnself;

}

 

- (void)loadInputView {

    UINib *inputViewNib = [UINib nibWithNibName:@"MusicScoreInputView" bundle:nil];

    [inputViewNib instantiateWithOwner:self options:nil];

}

 

@end

 

@interface MusicScoreInputView : UIView

 

@property (nonatomic, weak)IBOutlet MusicScoreView *delegate;

 

@end

 

@implementation MusicScoreInputView

 

@synthesize delegate;

 

视觉上的样式:反应在键盘上

现在,我们可以显示一个自定义的键盘,我们仍然需要确保当它出现调整我们的意见。由于实际演示,我们的输入视图是由UIKit处理,我们需要观察和反应框架发送到宣布在输入视图的位置变化的通知。苹果提供了一套  NSNotification我们可以观察到:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

这些通知中的每一个都包括一个用户信息字典描述的帧之前和之后它的过渡和动画的定时,将用于显示或隐藏键盘。当我们旋转设备的键盘,可能会调整,以更好地支持新的方向。在这种情况下,我们需要更新我们的其他意见的插图或立场以及以反映这些新的尺寸。UIKit会再次提供了一组的通知,我们可以观察到,以确定键盘的变化范围,并做出相应的反应。

  • UIKeyboardWillChangeFrameNotification
  • UIKeyboardDidChangeFrameNotification

考虑到这些,我们可以响应键盘按需要的出现或消失。

1
2
3
4
5
6
7
8
9

- (void)viewWillAppear:(BOOL)animated {

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateScrollInsets:) name:UIKeyboardWillChangeFrameNotification object:nil];

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

}

 

- (void)viewDidDisappear:(BOOL)animated {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}



在大多数情况下,我们将要应对 一个UIScrollView的的contentInset  和  scrollIndicatorInsets的调整   ,以补充足够的填充我们的滚动视图的内容视图中的所有内容可以滚动到上面的位置在键盘的顶部底部。另外,我们可能直接调整的一些看法的帧,但是这是不太理想的,因为它可能会留下一个空白区域,屏幕(而非显示滚动视图的背景颜色),尤其是iPad的地方,用户可以选择分裂键盘。

计算我们需要知道,键盘是呈现固定的底部的窗口,它是不是一定冲洗的当前观点控制器的观点(例如,我们可能有一个标签栏或工具栏可见底部适当的插图)。我们应该只加插图我们认为这是由键盘被隐藏的部分的高度相等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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

    //determine what portion of the view will be hidden by the keyboard

    CGRect keyboardEndFrameInScreenCoordinates;

    [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrameInScreenCoordinates];

    CGRect keyboardEndFrameInWindowCoordinates = [self.view.window convertRect:keyboardEndFrameInScreenCoordinates fromWindow:nil];

    CGRect keyboardEndFrameInViewCoordinates = [self.view convertRect:keyboardEndFrameInWindowCoordinates fromView:nil];

    CGRect windowFrameInViewCoords = [self.view convertRect:self.view.window.frame fromView:nil];

    CGFloat heightBelowViewInWindow = windowFrameInViewCoords.origin.y + windowFrameInViewCoords.size.height - (self.view.frame.origin.y +self.view.frame.size.height);

    CGFloat heightCoveredByKeyboard = keyboardEndFrameInViewCoordinates.size.height - heightBelowViewInWindow;

 

    //build an inset to add padding to the content view equal to the height of the portion of the view hidden by the keyboard

    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, heightCoveredByKeyboard, 0);

    [self setInsets:insets givenUserInfo:notification.userInfo];

}

 

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

    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);

    [self setInsets:insets givenUserInfo:notification.userInfo];

}

 

- (void) setInsets:(UIEdgeInsets)insets givenUserInfo:(NSDictionary *)userInfo {

    //match the keyboard's animation

    double duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

    UIViewAnimationOptions animationOptions = animationCurve;

    [UIView animateWithDuration:duration delay:0 options:animationOptions animations:^{

        self.textView.contentInset = insets;

        self.textView.scrollIndicatorInsets = insets;

    } completion:nil];

}


总结

我们已经看到了如何定义自定义输入和输入附件的意见,以增强或替换系统键盘。如何添加这些输入视图中现有的文本字段,文本的意见,和我们自己的自定义视图类。当一个输入视图中确实出现了,我们也可以适当调整作息我们的内容来容纳它。有了这些工具,我们现在应该准备建立我们所需要的数据的类型和背景,它会聚集量身定做的自定义的输入视图。

也可在上面给出的例子  https://jonah-carbonfive github.com /乔纳carbonfive的/ CustomInputViewDemo.git的

typedef enum { 
UIKeyboardTypeDefault, // 默认键盘:支持所有字符 
UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘 
UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,支持+*#等符号 
UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符 
UIKeyboardTypeNumberPad, //数字键盘 
UIKeyboardTypePhonePad, // 电话键盘 
UIKeyboardTypeNamePhonePad, // 电话键盘,也支持输入人名字 
UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘 
} UIKeyboardType; 
用法用例:
textView.keyboardtype = UIKeyboardTypeNumberPad;
二、键盘外观
typedef enum { 
UIKeyboardAppearanceDefault, // 默认外观:浅灰色 
UIKeyboardAppearanceAlert, //深灰/石墨色 
} UIKeyboardAppearance; 
用法用例:
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
三、回车键
typedef enum { 
UIReturnKeyDefault, //默认:灰色按钮,标有Return
UIReturnKeyGo, //标有Go的蓝色按钮
UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索
UIReturnKeyJoin, //标有Join的蓝色按钮
UIReturnKeyNext, //标有Next的蓝色按钮
UIReturnKeyRoute, //标有Route的蓝色按钮
UIReturnKeySearch, //标有Search的蓝色按钮
UIReturnKeySend, //标有Send的蓝色按钮
UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
UIReturnKeyDone, //标有Done的蓝色按钮
UIReturnKeyEmergencyCall, //紧急呼叫按钮
} UIReturnKeyType; 
用法用例:
textView.returnKeyType=UIReturnKeyGo;
四、自动大写
typedef enum { 
UITextAutocapitalizationTypeNone, //不自动大写 
UITextAutocapitalizationTypeWords, //单词首字母大写 
UITextAutocapitalizationTypeSentences, //句子首字母大写 
UITextAutocapitalizationTypeAllCharacters, //所有字母大写 
} UITextAutocapitalizationType; 
用法用例:
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值