日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在iOS8 下用Swift 创建自定义的键盘

發(fā)布時間:2023/12/18 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在iOS8 下用Swift 创建自定义的键盘 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文翻譯自How to make a custom keyboard in iOS 8 using Swift

我將講解一些關(guān)于鍵盤擴展的基本知識,然后使用iOS 8 提供的新應(yīng)用擴展API來創(chuàng)建一個莫斯碼鍵盤。大概需要你花20多分鐘來走完所有的步驟。 完整代碼

綜述

一個自定義的鍵盤會替換系統(tǒng)的鍵盤,來提供給用戶一個新的文本輸入方法,或者輸入哪些iOS系統(tǒng)還不支持的語言。一個自定義鍵盤的基本功能很簡單:響應(yīng)點擊,手勢或者其它輸入事件以及在當(dāng)前的文本輸入對象的文本插入點上提供非屬性化的NSString對象的文本。

當(dāng)用戶選擇了一個鍵盤,那么當(dāng)用戶打開一個app時,這個鍵盤會作為默認(rèn)的鍵盤顯示。因此這個鍵盤必須允許用戶切換到另一個鍵盤。

對于每個自定義鍵盤,有兩個開發(fā)要素:

信任 : 你的自定義鍵盤可以訪問用戶輸入的每個字符,所以你和你用戶之間的信任非常重要。

下一個鍵盤按鍵 : 能夠讓用戶切換另一個鍵盤這種可見性的功能應(yīng)該是一個鍵盤用戶界面的一部分;你必須提供這個切換功能。

注意:如果你只需要添加幾個按鈕到系統(tǒng)的鍵盤,你應(yīng)該查看 Custom Views for Data Input

一個自定義鍵盤不能夠做什么

有一些特定的輸入對象是你的自定義鍵盤沒資格輸入的:安全領(lǐng)域(例如密碼輸入框), 電話鍵盤對象(如在通訊錄中的電話號碼輸入框)。

你的自定義鍵盤不能訪問輸入視圖的層級結(jié)構(gòu),不能控制光標(biāo)和選擇文本。

另外,自定義鍵盤無法在頂行以上顯示任何東西(如系統(tǒng)鍵盤,當(dāng)你在頂行長按一個按鍵時)。

沙盒

默認(rèn)情況下,一個鍵盤是沒有網(wǎng)絡(luò)訪問權(quán)限的,而且也無法與鍵盤的容器app分享文件。為了獲得這些權(quán)限,可以在Info.plist 文件中設(shè)置 RequestsOpenAccess 這個布爾類型的鍵的值為 YES。 做這些會擴展鍵盤的沙盒,如 Establishing and Maintaining User Trust.中描述的。

如何你這么做來申請開放權(quán)限,你的鍵盤會獲得一下功能,每一個都伴隨著責(zé)任:

  • 訪問位置服務(wù)和 Address BOOK 數(shù)據(jù)庫,在第一次訪問時會要求申請用戶權(quán)限。

  • 可以與包含鍵盤的app共享一個容器,例如這樣可以允許在包含鍵盤的app里面提供一個自定義的詞庫管理界面。

  • 能夠發(fā)送鍵盤的點擊和其它輸入事件到服務(wù)端去處理。

  • 訪問iCloud,例如確保同一個用戶的鍵盤的設(shè)置和你的自動更正詞庫在所有設(shè)備上同步。

  • 通過包含鍵盤的app訪問Game Center 和 應(yīng)用內(nèi)購買。

  • 如果你設(shè)計你的鍵盤支持手機設(shè)備管理(MDM),那么還可以允許與管理的app一起工作。

確保你閱讀了Designing for User Trust,它描述了在你申請開放權(quán)限的情況下,你尊重和保護用戶數(shù)據(jù)的責(zé)任。

高層視圖

下面的圖片顯示了在一個運行的鍵盤中一些重要的對象,并且顯示了在一個典型的開發(fā)流程中這些對象來源于哪里。在一個最基本的形式中,我們有一個app包含了鍵盤擴展和一個控制這個鍵盤和響應(yīng)用戶事件的UIInputViewController對象。

這個自定義的鍵盤模版包含一個 UIInputViewController的子類,這是你的鍵盤的主視圖控制器。讓我們看看它的接口是怎么定義的:

class UIInputViewController : UIViewController, UITextInputDelegate, NSObjectProtocol {var inputView: UIInputView!var textDocumentProxy: NSObject! { get }func dismissKeyboard()func advanceToNextInputMode()// This will not provide a complete repository of a language's vocabulary.// It is solely intended to supplement existing lexicons.func requestSupplementaryLexiconWithCompletion(completionHandler: ((UILexicon!) -> Void)!) }
  • inputView 是這個鍵盤的視圖,與view屬性一樣

  • dismissKeyboard方法可以被調(diào)用來關(guān)閉鍵盤視圖

  • advanceToNextInputMode 是用來切換鍵盤的

  • textDocumentProxy 是你將用來與當(dāng)前的文本輸入進行交互的對象。

例如:

self.textDocumentProxy.insertText("We ? Swift") // inserts the string "We ? Swift" at the insertion pointself.textDocumentProxy.deleteBackward() // Deletes the character to the left of the insertion point
  • UIInputViewController 實現(xiàn)了UITextInputDelegate協(xié)議,當(dāng)文本或者選擇的文本發(fā)生變化時,會使用selectionWillChange , selectionDidChange, textWillChange 和 textDidChange 消息來通知你。

創(chuàng)建一個莫斯碼鍵盤

我們將創(chuàng)建一個簡單的鍵盤,可以輸入點和破折號,切換鍵盤,刪除一個字符以及關(guān)閉鍵盤。這個例子只通過代碼來創(chuàng)建用戶界面。我們也可以使用Nib 文件來創(chuàng)建界面-這個會在教程末尾涉及到。 加載Nibs 文件可能會對性能產(chǎn)生負(fù)面影響。

創(chuàng)建一個新的工程

打開Xcode 6, 創(chuàng)建一個新的“Single Page Application” 項目,選擇 Swift作為開發(fā)語言。

添加一個text field 文本框

打開 Main.storyboard ,然后從 Component Library 中拖動一個文本框。我們將在后面使用這個來測試我們的鍵盤。

把這個文本框居中,添加必要的約束。

暗示: 如果你在 viewDidLoad 中調(diào)用 textField.becomeFirstResponder() 那么當(dāng)你打開這個app時鍵盤就會打開。

添加鍵盤擴展

在navigator中選擇項目文件,點擊 + 號添加一個新target。

選擇 Application Extension ,使用 Custom Keyboard 模版, 命名為MorseCodeKeyboard。

這樣就會創(chuàng)建一個新的組,名叫 MorseCodeKeyboard,里面包含了兩個文件 KeyboardViewController.swift 和 Info.plist。

清理

打開 KeyboardViewController.swift 文件。這個模版鍵盤有一個已經(jīng)創(chuàng)建好的按鈕,用來進行切換鍵盤的。把這些代碼從 viewDidLoad 中移到一個新的方法 addNextKeyboardButton 中。

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

創(chuàng)建一個 addKeyboardButtons 方法,然后在 viewDidLoad 中調(diào)用它。這樣會有助于組織代碼。現(xiàn)在我們只是有了幾個按鈕,但是在實際的項目中會有更多的按鈕。 在 addKeyboardButtons 中調(diào)用 addNextKeyboardButton 。

class KeyboardViewController: UIInputViewController {...override func viewDidLoad() {super.viewDidLoad()addKeyboardButtons()}func addKeyboardButtons() {addNextKeyboardButton()}...}

現(xiàn)在添加點按鈕。 創(chuàng)建一個類型為 UIButton! 為的 dotButton 屬性。

class KeyboardViewController: UIInputViewController {var nextKeyboardButton: UIButton!var dotButton: UIButton!... }

添加一個 addDot 方法。 以一個系統(tǒng)類型的按鈕來初始化這個 dotButton 屬性。給 TouchUpInside 事件添加一個回調(diào)。 設(shè)置一個更大的字體和添加一個圓角。 添加約束來把這個按鈕放在離水平中心位置左邊 50個點,垂直居中的位置。 代碼與 nextKeyboardButton 的類似。

func addDot() {// initialize the buttondotButton = UIButton.buttonWithType(.System) as UIButtondotButton.setTitle(".", forState: .Normal)dotButton.sizeToFit()dotButton.setTranslatesAutoresizingMaskIntoConstraints(false)// adding a callbackdotButton.addTarget(self, action: "didTapDot", forControlEvents: .TouchUpInside)// make the font biggerdotButton.titleLabel.font = UIFont.systemFontOfSize(32)// add rounded cornersdotButton.backgroundColor = UIColor(white: 0.9, alpha: 1)dotButton.layer.cornerRadius = 5view.addSubview(dotButton)// makes the vertical centers equa;var dotCenterYConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)// set the button 50 points to the left (-) of the horizontal centervar dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: -50)view.addConstraints([dotCenterXConstraint, dotCenterYConstraint]) }

使用 textDocumentProxy實現(xiàn) dotButton 的回調(diào)。

func didTapDot() {var proxy = textDocumentProxy as UITextDocumentProxyproxy.insertText(".") }

在 addKeyboardButtons 中調(diào)用 addDot。

func addKeyboardButtons() {addDot()addNextKeyboardButton() }

對于 dash,delete, hideKeyboard 按鈕,過程類似。

破折號

代碼類似于 dotButton,為了把它對稱地放在水平中心位置,只需要改變水平約束的常量即可:

func addDash() {...// set the button 50 points to the left (-) of the horizontal centervar dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: -50)view.addConstraints([dashCenterXConstraint, dashCenterYConstraint]) }func didTapDash() {var proxy = textDocumentProxy as UITextDocumentProxyproxy.insertText("_") }

刪除按鈕

刪除按鈕會使用 deleteBackward 方法從 textDocumentProxy 中刪除一個字符。 這個布局約束與 nextKeyboardButton 對稱( .Left -> .Right, .Bottom-> .Top)。

func addDelete() {deleteButton = UIButton.buttonWithType(.System) as UIButtondeleteButton.setTitle(" Delete ", forState: .Normal)deleteButton.sizeToFit()deleteButton.setTranslatesAutoresizingMaskIntoConstraints(false)deleteButton.addTarget(self, action: "didTapDelete", forControlEvents: .TouchUpInside)deleteButton.backgroundColor = UIColor(white: 0.9, alpha: 1)deleteButton.layer.cornerRadius = 5view.addSubview(deleteButton)var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0)view.addConstraints([rightSideConstraint, topConstraint]) }func didTapDelete() {var proxy = textDocumentProxy as UITextDocumentProxyproxy.deleteBackward() }

隱藏鍵盤

hideKeyboardButton 會在點擊時,調(diào)用 dismissKeyboard 來隱藏鍵盤:

func addHideKeyboardButton() {hideKeyboardButton = UIButton.buttonWithType(.System) as UIButtonhideKeyboardButton.setTitle("Hide Keyboard", forState: .Normal)hideKeyboardButton.sizeToFit()hideKeyboardButton.setTranslatesAutoresizingMaskIntoConstraints(false)hideKeyboardButton.addTarget(self, action: "dismissKeyboard", forControlEvents: .TouchUpInside)view.addSubview(hideKeyboardButton)var rightSideConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)var bottomConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: -10.0)view.addConstraints([rightSideConstraint, bottomConstraint]) }

使用 Nib 文件

為了不用手寫這些布局約束,你可以創(chuàng)建一個界面文件,然后直接在上面添加約束。

創(chuàng)建一個界面文件

右擊 MorseCodeKeyboard組,然后選擇 New File.

選擇 User Interface 和 View 模版。 命名為 CustomKeyboardInterface

選擇 File's Owner ,改變類名為 KeyboardViewController

在視圖中添加一個按鈕,設(shè)置標(biāo)題為 We ? Swift 。 界面類似下面這樣:

加載界面

在 init(nibName, bundle) 構(gòu)造器中加載 CustomKeyboard nib 文件

class KeyboardViewController: UIInputViewController {...var customInterface: UIView!init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)var nib = UINib(nibName: "CustomKeyBoardInterface", bundle: nil)let objects = nib.instantiateWithOwner(self, options: nil)customInterface = objects[0] as UIView}...}

添加到 inputView

在 viewDidLoad 方法中,添加自定義的界面到inputView中。

class KeyboardViewController: UIInputViewController {...override func viewDidLoad() {super.viewDidLoad()view.addSubview(customInterface)...}... }

給按鈕添加一個回調(diào)

class KeyboardViewController: UIInputViewController {...@IBAction func didTapWeheartSwift() {var proxy = textDocumentProxy as UITextDocumentProxyproxy.insertText("We ? Swift")}... }

連接按鈕的事件到這個回調(diào)上

右鍵這個按鈕,然后點擊 touchUpInside 并拖動到 didTapWeHeartSwift 這個 IBAction中

最后,代碼應(yīng)該是這樣的。

在你的設(shè)備上安裝這個容器app

在你的設(shè)備上運行這個app后,如下來添加你的自定義鍵盤:

選擇鍵盤。

選擇添加一個新鍵盤。找到我們的 MorseCode鍵盤:

現(xiàn)在重新運行我們的應(yīng)用,盡情享受我們的新鍵盤吧。

轉(zhuǎn)載于:https://www.cnblogs.com/YungMing/p/4492869.html

總結(jié)

以上是生活随笔為你收集整理的在iOS8 下用Swift 创建自定义的键盘的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。