iOS 8自定义键盘的库:Slidden

jopen 9年前

Slidden 是开源,可定制的,使用 Swift 编写的 iOS 8 键盘。利用这个开源库,可以方便的配置键位、颜色以及键位对应的图片。当然,如果你想学习如何一步一步开发自定义键盘

Simple

If you subclass Slidden.KeyboardViewController, you get a KeyboardView and nice autolayout constraints right out of the box. Subclassing is as easy as:

class KeyboardViewController: Slidden.KeyboardViewController {      override func viewDidLoad() {          super.viewDidLoad()            // Add the keys we need to the keyboard          setupKeys()      }

Add keys to the keyboard.

func setupKeys() {          let helloKey = KeyboardKeyView(type: .Character, keyCap: "Hello", outputText: "Hello")          helloKey.textColor = UIColor.whiteColor()          helloKey.color = UIColor.blueColor()          self.keyboardView.addKey(helloKey, row: 0)            let worldKey = KeyboardKeyView(type: .Character, keyCap: "World", outputText: "World")          worldKey.textColor = UIColor.whiteColor()          worldKey.color = UIColor.redColor()          self.keyboardView.addKey(worldKey, row: 0)  }

Add images to your keys.

func setupKeys() {      let shiftKey = KeyboardKeyView(type: .Shift, keyCap:"", outputText: "")      let img = UIImage(named:"Shift")      shiftKey.image = img      shiftKey.imageView.contentMode = .Center  }

If you want your image to be the same color as your other keys' text, you can use shouldColorImage to have CoreGraphics redraw the image of your key with the uniform color.

func setupKeys() {      let shiftKey = KeyboardKeyView(type: .Shift, keyCap:"", outputText: "")      let img = UIImage(named:"Shift")        shiftKey.image = img      shiftKey.imageView.contentMode = .Center        shiftKey.color = UIColor.blueColor()      shiftKey.selectedColor = UIColor.darkerBlueColor()      shiftKey.textColor = UIColor.whiteColor()      shiftKey.shouldColorImage = true // Will redraw the Shift image to match `textColor`  }

iOS 8 键盘:Slidden

项目主页:http://www.open-open.com/lib/view/home/1412906355327