首页 >> 大全

iOS输入法开发(Swift)

2023-07-08 大全 31 作者:考证青年

我会复习一下有关键盘扩展的内容,然后通过使用iOS 8中的新应用扩展API的设计一个摩斯码的输入法。完成这个教程大约需要花费20分钟。 完整代码

概览

通过使用自定义输入法替换系统输入法,用户可以实现一些特别的功能。例如一个特别新颖的输入方式,或输入iOS原生并不支持的语言。自定义输入法的基本功能很简单:通过点击、手势,或者其他输入事件,然后通过一个未分类的 对象在当前文本输入对象的文本插入点插入文字。

当用户选择了某个输入法后,当应用打开时,它将会变为默认的输入法。因此,任意输入法都应该允许用户切换到另一个输入法。

对于每一个自定义输入法,有两个很重要的点:

1、信任: 你的自定义输入法能够让你访问用户输入的所有内容,因此你和你的用户之间的信任非常重要。

2、一个“下一个输入法”选项: 兼容能够让用户切换至另一个输入法必须成为每个自定义输入法界面的一部分。你必须在你的界面中中提供。

注意:如果你只是想在系统键盘中加上一些按钮,那么你应该研究一下 自定义数据输入视图。

自定义输入法所不能实现的

有一些输入对象是自定义输入法不能被使用的情况:安全字段(例如密码),电话号码对象(就像联系人应用中的号码字段)。

你的自定义输入法不能访问输入框的视图层级,它也不能控制光标和选择文本。

同样,自定义输入法不能在顶行之外显示任何内容(就像系统键盘当你在后面的行上长按一个键时)。

沙盒

默认情况下,自定义输入法并没有网络访问,也不能和容纳它的应用共享文件。如果想实现这些功能,必须在Info.plist文件中将布尔值至YES。做了这个之后,会扩展自定义输入法的沙盒,就像在 建立和维护用户信任提到的那样。

如果你确实需要申请访问权限,你的输入法将获得以下权限,每一个都会有相应的责任:

1.访问位置服务和地址本数据库,每一个都需在第一次访问时获得授权。

2.可选择与容纳该输入法的应用的共享容器,使得在应用中可以定制词汇表。

3.能够将输入的字符和其他输入事件上传至服务器进行处理。

4.访问,例如,能够确保输入法的设置以及你的自动修正词汇能够在所有用户设备上同步。

5.通过包含还输入法的应用,能够访问Game 和应用内购买。

6.能够和受控应用进行协同,如果你使用来设计该键盘以支持移动设备管理(MDM)

如果你确实要打开这些权限,确保你阅读” 为用户信任而设计“这篇文章,它介绍了你的责任以集如何尊重和保护用户的数据。

顶层设计

接下来的图形展示了在运行的输入法下有哪些重要的元素,同时展示了在一个典型的开发流程中,他们的位置。在大部分情况下,我们通过一个应用来容纳这个输入法扩展,一个r来控制这个键盘并针对用户事件给予反馈。

自定义的输入法模板包含一个 r 的子类,这个就是你的输入法的主要视图控制器。让我们看一下他的接口来熟悉一下是如何实现的:

 
  1. class UIInputViewController : UIViewController, UITextInputDelegate, NSObjectProtocol { 
  2.  
  3. var inputView: UIInputView! 
  4.  
  5. var textDocumentProxy: NSObject! { get } 
  6.  
  7. func dismissKeyboard() 
  8. func advanceToNextInputMode() 
  9.  
  10. // This will not provide a complete repository of a language's vocabulary. 
  11. // It is solely intended to supplement existing lexicons. 
  12. func requestSupplementaryLexiconWithCompletion(completionHandler: ((UILexicon!) -> Void)!) 

r 支持 协议,当文本区或者文本选项区变化时,通过 ,, 和 事件来实现。

设计一个摩斯码输入法

我们会设计实现一个简单的输入法,可以输入 点 和 划,改变了键盘结构,删除字符然后隐藏自己。这个范例通过代码来生成的用户界面。当然,我们同样也可以使用Nib文件来生成界面-这个会在教程的末尾涉及。加载Nib文件会对性能有负面影响。

创建一个工程

打开 ,创建一个“ Page ”,然后选择Swift为编程语言。

添加一个文本区域

打开Main.然后拖拽一个文本区域从组件库里。我们会使用这个来测试我们设计的键盘。

将这个文本区域居中,然后添加必要的。

Hint: 如果你调用 .()在 ,这个键盘会自动在应用打开时弹出。

添加这个键盘扩展

从导航器中选择这个项目文件,然后通过按+按钮添加一个新的。

选择 然后使用 模板,命名它为。

这会创建一个名为 新文件夹,包括2个文件 er.swift 和 Info.Plist。

接下来

打开 er.swift,为了在不同的键盘之间进行切换,这个键盘模板文件会有一个按钮。在 方法中放入一个新的方法,命名为 n。

 
  1. func addNextKeyboardButton() { 
  2.     self.nextKeyboardButton = UIButton.buttonWithType(.System) as UIButton 
  3.  
  4.     ... 
  5.  
  6.     var nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: -10.0) 
  7.     self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]) 

为了更好的梳理代码的结构,创建一个新的方法名为,然后在 中调用它。虽然这里只有几个按钮,但是在真实项目中,将会有更多的按钮。在 中调用 n。

 
  1. class KeyboardViewController: UIInputViewController { 
  2.  
  3. ... 
  4.  
  5. override func viewDidLoad() { 
  6.     super.viewDidLoad() 
  7.  
  8.     addKeyboardButtons() 
  9.  
  10. func addKeyboardButtons() { 
  11.     addNextKeyboardButton() 
  12.  
  13. ... 
  14.  

现在我们来添加点按钮,创建一个!类型的属性。

 
  1. class KeyboardViewController: UIInputViewController { 
  2.  
  3. var nextKeyboardButton: UIButton! 
  4. var dotButton: UIButton! 
  5.  
  6. ... 

增加一个名为 的方法。使用系统按钮来初始化名为 的属性。增加一个 事件回调函数。设置一个大字体然后增加圆角,同时增加约束来限制它距离水平中心50个,垂直居中。这个代码应该和下面部分类似。

 
  1. func addDot() { 
  2. // initialize the button 
  3. dotButton = UIButton.buttonWithType(.System) as UIButton 
  4. dotButton.setTitle(".", forState: .Normal) 
  5. dotButton.sizeToFit() 
  6. dotButton.setTranslatesAutoresizingMaskIntoConstraints(false
  7.  
  8. // adding a callback 
  9. dotButton.addTarget(self, action: "didTapDot", forControlEvents: .TouchUpInside) 
  10.  
  11. // make the font bigger 
  12. dotButton.titleLabel.font = UIFont.systemFontOfSize(32) 
  13.  
  14. // add rounded corners 
  15. dotButton.backgroundColor = UIColor(white: 0.9, alpha: 1) 
  16. dotButton.layer.cornerRadius = 5 
  17.  
  18. view.addSubview(dotButton) 
  19.  
  20. // makes the vertical centers equa; 
  21. var dotCenterYConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0) 
  22.  
  23. // set the button 50 points to the left (-) of the horizontal center 
  24. var dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: -50) 
  25.  
  26. view.addConstraints([dotCenterXConstraint, dotCenterYConstraint]) 

接下来对于dash, 和 ,这个过程都比较类似。这个会从proxy使用方法,然后会通过er使用方法。

与dash相关的代码几乎和代码一致。为了将按钮在水平方向与 点 按钮对称,只要将水平约束中的常量改变一下符号即可。

 
  1. func addDash() { 
  2. ... 
  3.  
  4. // set the button 50 points to the left (-) of the horizontal center 
  5. var dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: -50) 
  6.  
  7. view.addConstraints([dashCenterXConstraint, dashCenterYConstraint]) 

回删

当被按下时,这个删除按钮会通过,使用删除字符。这个布局约束会和对称(.Left -> .Right, .->.Top)

 
  1. func addDelete() { 
  2. deleteButton = UIButton.buttonWithType(.System) as UIButton 
  3. deleteButton.setTitle(" Delete ", forState: .Normal) 
  4. deleteButton.sizeToFit() 
  5. deleteButton.setTranslatesAutoresizingMaskIntoConstraints(false
  6. deleteButton.addTarget(self, action: "didTapDelete", forControlEvents: .TouchUpInside) 
  7.  
  8. deleteButton.backgroundColor = UIColor(white: 0.9, alpha: 1) 
  9. deleteButton.layer.cornerRadius = 5 
  10.  
  11. view.addSubview(deleteButton) 
  12.  
  13. var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0) 
  14.  
  15. var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0) 
  16.  
  17. view.addConstraints([rightSideConstraint, topConstraint]) 

隐藏键盘

当被按下时,这个会在er上调用。

 
  1. func addHideKeyboardButton() { 
  2. hideKeyboardButton = UIButton.buttonWithType(.System) as UIButton 
  3.  
  4. hideKeyboardButton.setTitle("Hide Keyboard", forState: .Normal) 
  5. hideKeyboardButton.sizeToFit() 
  6. hideKeyboardButton.setTranslatesAutoresizingMaskIntoConstraints(false
  7.  
  8. hideKeyboardButton.addTarget(self, action: "dismissKeyboard", forControlEvents: .TouchUpInside) 
  9.  
  10. view.addSubview(hideKeyboardButton) 
  11.  
  12. var rightSideConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0) 
  13.  
  14. var bottomConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: -10.0) 
  15.  
  16. view.addConstraints([rightSideConstraint, bottomConstraint]) 

使用Nib文件

_输入法swiftkey_输入法开发教程

如果写约束并非你擅长的方式,你可以创建一个界面文件,然后将它添加到你的。

创建一个界面文件

右击文件组,然后选择创建新文件。

选择User ,然后View ,命名为ace。

选择File’s Owner,然后在 标签下,将类的名字改为er。

在视图中加入一个按钮,然后将Title设置为We ? Swift。这个界面应该和这个有点相似:

加载界面

在 init(, ) 构造函数内,加载这个 文件,然后保存一个对于这个界面的引用。

 
  1. class KeyboardViewController: UIInputViewController { 
  2.  
  3. ... 
  4.  
  5. var customInterface: UIView! 
  6.  
  7. init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 
  8.     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
  9.  
  10.     var nib = UINib(nibName: "CustomKeyBoardInterface", bundle: nil) 
  11.     let objects = nib.instantiateWithOwner(self, options: nil) 
  12.     customInterface = objects[0] as UIView 
  13.  
  14. ... 
  15.  

将它添加到

在 方法中,将这个自定义界面添加到 。

 
  1. class KeyboardViewController: UIInputViewController { 
  2.  
  3. ... 
  4.  
  5. override func viewDidLoad() { 
  6.     super.viewDidLoad() 
  7.  
  8.     view.addSubview(customInterface) 
  9.  
  10.     ... 
  11.  
  12. ... 
  13.  

为按钮添加回调函数

 
  1. class KeyboardViewController: UIInputViewController { 
  2.  
  3. ... 
  4.  
  5. @IBAction func didTapWeheartSwift() { 
  6.     var proxy = textDocumentProxy as UITextDocumentProxy 
  7.  
  8.     proxy.insertText("We ? Swift"
  9.  
  10. ... 
  11.  

将按钮事件绑定到回调函数

在按钮上右击,然后单击并拖拽 事件到 。

最后,这个代码应该和这个差不多。

在你的设备上安装应用

在设备上运行你的项目。他会增加一个自定义键盘在你的设备上,但是在你使用它之前,你必须安装它。

进入设置>通用。选择键盘-应该在选项的底部。

选择键盘。

选择增加新键盘。然后你会看到的键盘,选择然后安装这个键盘。

现在我们可以再次运行这个应用,然后使用你的键盘了。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了