Skip to content

Commit

Permalink
Support UITextView.
Browse files Browse the repository at this point in the history
  • Loading branch information
lm2343635 committed Aug 12, 2017
1 parent cee4811 commit 8140ce6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 42 deletions.
22 changes: 20 additions & 2 deletions Example/MGKeyboardAccessory/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -62,20 +62,38 @@
<userDefinedRuntimeAttribute type="string" keyPath="strings" value="!@#$%^"/>
</userDefinedRuntimeAttributes>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="UITextField" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bIX-6c-Fay">
<rect key="frame" x="16" y="218" width="86" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="xY7-yx-Xh1">
<rect key="frame" x="16" y="247" width="343" height="400"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
</subviews>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<constraints>
<constraint firstItem="OBv-c8-Q6e" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="131" id="Bqs-Fz-md6"/>
<constraint firstAttribute="trailingMargin" secondItem="OBv-c8-Q6e" secondAttribute="trailing" id="IcM-bZ-KbG"/>
<constraint firstItem="bLg-6D-fXM" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="37" id="Su4-vb-VKY"/>
<constraint firstItem="bLg-6D-fXM" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="WCE-vM-e4F"/>
<constraint firstAttribute="trailingMargin" secondItem="xY7-yx-Xh1" secondAttribute="trailing" id="iYT-XL-2MJ"/>
<constraint firstItem="xY7-yx-Xh1" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="227" id="izB-FW-jKr"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="xY7-yx-Xh1" secondAttribute="bottom" constant="20" id="lGT-WI-cOz"/>
<constraint firstAttribute="leadingMargin" secondItem="OBv-c8-Q6e" secondAttribute="leading" id="ngq-Pn-Z99"/>
<constraint firstAttribute="trailingMargin" secondItem="bLg-6D-fXM" secondAttribute="trailing" id="quX-tH-4S8"/>
<constraint firstAttribute="leadingMargin" secondItem="xY7-yx-Xh1" secondAttribute="leading" id="zqs-Ik-2H6"/>
</constraints>
</view>
<connections>
<outlet property="blackTextField" destination="bLg-6D-fXM" id="sIG-oS-KL0"/>
<outlet property="defaultTextField" destination="OBv-c8-Q6e" id="tlZ-bN-mMe"/>
<outlet property="textView" destination="xY7-yx-Xh1" id="iAo-p1-wVD"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
Expand Down
2 changes: 2 additions & 0 deletions Example/MGKeyboardAccessory/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class ViewController: UIViewController {

@IBOutlet weak var blackTextField: UITextField!
@IBOutlet weak var defaultTextField: UITextField!
@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
super.viewDidLoad()
blackTextField.becomeFirstResponder()
blackTextField.setupKeyboardAccessory([":", "/", "?", "&", "alice", "=", "*", "-", "@", "~"], barStyle: .black)
defaultTextField.setupKeyboardAccessory([":", "/", "?", "&", ".", "="], barStyle: .default)
textView.setupKeyboardAccessory([":", "/", "?", "&", ".", "="], barStyle: .black)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// UITextField+KeyboardAccessory.swift
// AccessoryToolbar.swift
// MGKeyboardAccessory
//
// Created by limeng on 01/22/2017.
// Copyright (c) 2017 fczm.pw. All rights reserved.
// Created by Meng Li on 12/08/2017.
//
//

// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -26,41 +26,41 @@

import Foundation

public extension UITextField {
class AccessoryToolbar: UIToolbar {

private var textInput: UITextInput!

func setupKeyboardAccessory(_ strings: [String], barStyle: UIBarStyle) {
public init(_ strings: [String], barStyle: UIBarStyle, forTextInput: UITextInput) {
super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35))
self.textInput = forTextInput;
self.barStyle = barStyle;
let buttonColor = (barStyle == .default) ? UIColor.darkGray : UIColor.white
let clearButtonItem = UIBarButtonItem(barButtonSystemItem: .trash,
target: self,
action: #selector(clearTextFeild))
clearButtonItem.tintColor = buttonColor
let spaceButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
target: self,
action: nil)
let doneButtonItem = UIBarButtonItem(barButtonSystemItem: .done,
target: self,
action: #selector(editFinish))
doneButtonItem.width = 150;
doneButtonItem.tintColor = buttonColor

let topView: UIToolbar = {
let toolbar = UIToolbar.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 35))
toolbar.barStyle = barStyle;
let clearButtonItem = UIBarButtonItem(barButtonSystemItem: .trash,
target: self,
action: #selector(clearTextFeild))
clearButtonItem.tintColor = buttonColor
let spaceButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
target: self,
action: nil)
let doneButtonItem = UIBarButtonItem(barButtonSystemItem: .done,
target: self,
action: #selector(editFinish))
doneButtonItem.width = 150;
doneButtonItem.tintColor = buttonColor

let items = [clearButtonItem,
spaceButtonItem,
createStringBarButtonItem(strings: strings,
color: buttonColor,
action: #selector(addText(_:)),
height: 26),
spaceButtonItem,
doneButtonItem]
toolbar.setItems(items, animated: false)

return toolbar
}()

self.inputAccessoryView = topView
let items = [clearButtonItem,
spaceButtonItem,
createStringBarButtonItem(strings: strings,
color: buttonColor,
action: #selector(addText(_:)),
height: 26),
spaceButtonItem,
doneButtonItem]
self.setItems(items, animated: false)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func createStringBarButtonItem(strings: [String], color: UIColor, action: Selector, height: CGFloat) -> UIBarButtonItem {
Expand Down Expand Up @@ -101,18 +101,43 @@ public extension UITextField {
}

func addText(_ sender: UIButton) {
self.insertText((sender.titleLabel?.text)!)
if textInput.isMember(of: UITextField.self){
let textFiled = textInput as! UITextField
textFiled.insertText((sender.titleLabel?.text)!)
}
if textInput.isMember(of: UITextView.self) {
let textView = textInput as! UITextView
textView.insertText((sender.titleLabel?.text)!)
}
}

func editFinish() {
if self.isFirstResponder {
self.resignFirstResponder()
if textInput.isMember(of: UITextField.self){
let textFiled = textInput as! UITextField
if textFiled.isFirstResponder {
textFiled.resignFirstResponder()
}
}
if textInput.isMember(of: UITextView.self){
let textView = textInput as! UITextView
if textView.isFirstResponder {
textView.resignFirstResponder()
}
}
}

func clearTextFeild() {
if self.isFirstResponder {
self.text = ""
if textInput.isMember(of: UITextField.self){
let textFiled = textInput as! UITextField
if textFiled.isFirstResponder {
textFiled.text = ""
}
}
if textInput.isMember(of: UITextView.self){
let textView = textInput as! UITextView
if textView.isFirstResponder {
textView.text = ""
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions MGKeyboardAccessory/Classes/MGKeyboardAccessory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// MGKeyboardAccessory.swift
// MGKeyboardAccessory
//
// Created by Meng Li on 01/22/2017.
// Copyright (c) 2017 fczm.pw. All rights reserved.
//

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

public extension UITextField {

func setupKeyboardAccessory(_ strings: [String], barStyle: UIBarStyle) {
self.inputAccessoryView = AccessoryToolbar.init(strings, barStyle: barStyle, forTextInput: self)
}

}

public extension UITextView {

func setupKeyboardAccessory(_ strings: [String], barStyle: UIBarStyle) {
self.inputAccessoryView = AccessoryToolbar.init(strings, barStyle: barStyle, forTextInput: self)
}

}

0 comments on commit 8140ce6

Please sign in to comment.