Skip to content

Commit 390cb56

Browse files
authored
Merge pull request #68 from muukii/muukii/revise
Revise API and Code
2 parents 6f3ecf2 + 51d7b48 commit 390cb56

File tree

7 files changed

+106
-80
lines changed

7 files changed

+106
-80
lines changed

Demo/Base.lproj/Main.storyboard

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
47
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
69
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
710
</dependencies>
811
<scenes>
@@ -19,13 +22,16 @@
1922
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2023
<subviews>
2124
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ckE-F6-idm">
25+
<rect key="frame" x="0.0" y="619" width="375" height="48"/>
2226
<subviews>
2327
<view clipsSubviews="YES" contentMode="scaleToFill" placeholderIntrinsicWidth="300" placeholderIntrinsicHeight="36" translatesAutoresizingMaskIntoConstraints="NO" id="jTC-nR-hYx" customClass="NextGrowingTextView" customModule="NextGrowingTextView">
28+
<rect key="frame" x="8" y="6" width="314" height="36"/>
2429
<color key="backgroundColor" red="0.94797950983047485" green="0.94797950983047485" blue="0.94797950983047485" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2530
</view>
26-
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="atN-J5-w3s">
31+
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="1000" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="atN-J5-w3s">
32+
<rect key="frame" x="329" y="14" width="38" height="28"/>
2733
<constraints>
28-
<constraint firstAttribute="height" constant="36" id="0Tx-Ge-BAv"/>
34+
<constraint firstAttribute="height" constant="28" id="0Tx-Ge-BAv"/>
2935
</constraints>
3036
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
3137
<state key="normal" title="Send"/>
@@ -36,12 +42,12 @@
3642
</subviews>
3743
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3844
<constraints>
39-
<constraint firstAttribute="bottom" secondItem="jTC-nR-hYx" secondAttribute="bottom" constant="4" id="2bO-GH-WID"/>
45+
<constraint firstAttribute="bottom" secondItem="jTC-nR-hYx" secondAttribute="bottom" constant="6" id="2bO-GH-WID"/>
4046
<constraint firstAttribute="trailing" secondItem="atN-J5-w3s" secondAttribute="trailing" constant="8" id="YJc-pr-5mz"/>
4147
<constraint firstItem="jTC-nR-hYx" firstAttribute="leading" secondItem="ckE-F6-idm" secondAttribute="leading" constant="8" id="cYG-ik-yA5"/>
4248
<constraint firstItem="atN-J5-w3s" firstAttribute="leading" secondItem="jTC-nR-hYx" secondAttribute="trailing" constant="7" id="cwv-d8-157"/>
43-
<constraint firstItem="jTC-nR-hYx" firstAttribute="top" secondItem="ckE-F6-idm" secondAttribute="top" constant="4" id="mMp-Aj-i3w"/>
44-
<constraint firstAttribute="bottom" secondItem="atN-J5-w3s" secondAttribute="bottom" constant="4" id="sDY-fm-ocO"/>
49+
<constraint firstItem="jTC-nR-hYx" firstAttribute="top" secondItem="ckE-F6-idm" secondAttribute="top" constant="6" id="mMp-Aj-i3w"/>
50+
<constraint firstAttribute="bottom" secondItem="atN-J5-w3s" secondAttribute="bottom" constant="6" id="sDY-fm-ocO"/>
4551
<constraint firstItem="jTC-nR-hYx" firstAttribute="leading" secondItem="ckE-F6-idm" secondAttribute="leading" constant="8" id="u1U-9A-s3M"/>
4652
</constraints>
4753
</view>
@@ -61,7 +67,7 @@
6167
</viewController>
6268
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
6369
</objects>
64-
<point key="canvasLocation" x="334.5" y="312.5"/>
70+
<point key="canvasLocation" x="333.60000000000002" y="311.69415292353824"/>
6571
</scene>
6672
</scenes>
6773
</document>

Demo/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<key>UISupportedInterfaceOrientations</key>
3434
<array>
3535
<string>UIInterfaceOrientationPortrait</string>
36+
<string>UIInterfaceOrientationLandscapeLeft</string>
37+
<string>UIInterfaceOrientationLandscapeRight</string>
3638
</array>
3739
</dict>
3840
</plist>

Demo/ViewController.swift

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,63 @@ import UIKit
2424
import NextGrowingTextView
2525

2626
class ViewController: UIViewController {
27-
28-
@IBOutlet weak var inputContainerView: UIView!
29-
@IBOutlet weak var inputContainerViewBottom: NSLayoutConstraint!
30-
@IBOutlet weak var growingTextView: NextGrowingTextView!
31-
32-
override func viewDidLoad() {
33-
super.viewDidLoad()
34-
// Do any additional setup after loading the view, typically from a nib.
35-
36-
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
37-
38-
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
39-
40-
self.growingTextView.layer.cornerRadius = 4
41-
self.growingTextView.backgroundColor = UIColor(white: 0.9, alpha: 1)
42-
self.growingTextView.textView.textContainerInset = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
43-
self.growingTextView.placeholderAttributedText = NSAttributedString(string: "Placeholder text",
44-
attributes: [NSAttributedStringKey.font: self.growingTextView.textView.font!,
45-
NSAttributedStringKey.foregroundColor: UIColor.gray
46-
]
47-
)
48-
49-
}
50-
51-
override func didReceiveMemoryWarning() {
52-
super.didReceiveMemoryWarning()
53-
// Dispose of any resources that can be recreated.
54-
}
55-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
56-
self.view.endEditing(true)
57-
}
58-
59-
60-
@IBAction func handleSendButton(_ sender: AnyObject) {
61-
self.growingTextView.textView.text = ""
62-
self.view.endEditing(true)
63-
}
64-
65-
27+
28+
@IBOutlet weak var inputContainerView: UIView!
29+
@IBOutlet weak var inputContainerViewBottom: NSLayoutConstraint!
30+
@IBOutlet weak var growingTextView: NextGrowingTextView!
31+
32+
override func viewDidLoad() {
33+
super.viewDidLoad()
34+
// Do any additional setup after loading the view, typically from a nib.
35+
36+
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
37+
38+
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
39+
40+
self.growingTextView.layer.cornerRadius = 4
41+
self.growingTextView.backgroundColor = UIColor(white: 0.9, alpha: 1)
42+
self.growingTextView.placeholderAttributedText = NSAttributedString(
43+
string: "Placeholder text",
44+
attributes: [
45+
.font: self.growingTextView.textView.font!,
46+
.foregroundColor: UIColor.gray
47+
]
48+
)
49+
}
50+
51+
override func didReceiveMemoryWarning() {
52+
super.didReceiveMemoryWarning()
53+
// Dispose of any resources that can be recreated.
54+
}
55+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
56+
self.view.endEditing(true)
57+
}
58+
59+
60+
@IBAction func handleSendButton(_ sender: AnyObject) {
61+
self.growingTextView.textView.text = ""
62+
self.view.endEditing(true)
63+
}
64+
6665
@objc func keyboardWillHide(_ sender: Notification) {
67-
if let userInfo = (sender as NSNotification).userInfo {
68-
if let _ = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height {
69-
//key point 0,
70-
self.inputContainerViewBottom.constant = 0
71-
//textViewBottomConstraint.constant = keyboardHeight
72-
UIView.animate(withDuration: 0.25, animations: { () -> Void in self.view.layoutIfNeeded() })
73-
}
74-
}
66+
if let userInfo = (sender as NSNotification).userInfo {
67+
if let _ = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height {
68+
//key point 0,
69+
self.inputContainerViewBottom.constant = 0
70+
//textViewBottomConstraint.constant = keyboardHeight
71+
UIView.animate(withDuration: 0.25, animations: { () -> Void in self.view.layoutIfNeeded() })
72+
}
7573
}
74+
}
7675
@objc func keyboardWillShow(_ sender: Notification) {
77-
if let userInfo = (sender as NSNotification).userInfo {
78-
if let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height {
79-
self.inputContainerViewBottom.constant = keyboardHeight
80-
UIView.animate(withDuration: 0.25, animations: { () -> Void in
81-
self.view.layoutIfNeeded()
82-
})
83-
}
84-
}
76+
if let userInfo = (sender as NSNotification).userInfo {
77+
if let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size.height {
78+
self.inputContainerViewBottom.constant = keyboardHeight
79+
UIView.animate(withDuration: 0.25, animations: { () -> Void in
80+
self.view.layoutIfNeeded()
81+
})
82+
}
8583
}
84+
}
8685
}
8786

NextGrowingTextView.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
4BDB87F11DC0425600E70D5B /* NextGrowingTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = NextGrowingTextView.swift; sourceTree = "<group>"; tabWidth = 2; };
5252
4BDB87F81DC0426A00E70D5B /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
5353
4BDB87FA1DC0426A00E70D5B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
54-
4BDB87FC1DC0426A00E70D5B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
54+
4BDB87FC1DC0426A00E70D5B /* ViewController.swift */ = {isa = PBXFileReference; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; tabWidth = 2; };
5555
4BDB87FF1DC0426A00E70D5B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
5656
4BDB88011DC0426A00E70D5B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5757
4BDB88061DC0426A00E70D5B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

NextGrowingTextView/NextGrowingInternalTextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@ internal class NextGrowingInternalTextView: UITextView {
119119
}
120120

121121
private func updatePlaceholder() {
122-
displayPlaceholder = text.characters.count == 0
122+
displayPlaceholder = text.isEmpty
123123
}
124124
}

NextGrowingTextView/NextGrowingTextView.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import Foundation
2424
import UIKit
2525

26-
// MARK: - NextGrowingTextView: UIScrollView
27-
2826
open class NextGrowingTextView: UIScrollView {
2927

3028
// MARK: - Nested types
@@ -48,11 +46,9 @@ open class NextGrowingTextView: UIScrollView {
4846
}
4947
set {
5048
guard newValue > 1 else {
51-
_minHeight = simulateHeight(1)
49+
_minNumberOfLines = 1
5250
return
5351
}
54-
55-
_minHeight = simulateHeight(newValue)
5652
_minNumberOfLines = newValue
5753
}
5854
}
@@ -63,16 +59,19 @@ open class NextGrowingTextView: UIScrollView {
6359
}
6460
set {
6561
guard newValue > 1 else {
66-
_maxHeight = simulateHeight(1)
62+
_maxNumberOfLines = 1
6763
return
6864
}
69-
70-
_maxHeight = simulateHeight(newValue)
7165
_maxNumberOfLines = newValue
7266
}
7367
}
7468

75-
open var disableAutomaticScrollToBottom = false
69+
@available(*, deprecated, message: "Use isAutomaticScrollToBottomEnabled")
70+
open var disableAutomaticScrollToBottom: Bool {
71+
return !isAutomaticScrollToBottomEnabled
72+
}
73+
74+
open var isAutomaticScrollToBottomEnabled = true
7675

7776
open var placeholderAttributedText: NSAttributedString? {
7877
get { return _textView.placeholderAttributedText }
@@ -105,8 +104,19 @@ open class NextGrowingTextView: UIScrollView {
105104
}
106105

107106
private let _textView: NextGrowingInternalTextView
108-
private var _maxNumberOfLines: Int = 0
109-
private var _minNumberOfLines: Int = 0
107+
108+
private var _maxNumberOfLines: Int = 3 {
109+
didSet {
110+
_maxHeight = simulateHeight(_maxNumberOfLines)
111+
}
112+
}
113+
114+
private var _minNumberOfLines: Int = 1 {
115+
didSet {
116+
_minHeight = simulateHeight(_minNumberOfLines)
117+
}
118+
}
119+
110120
private var _maxHeight: CGFloat = 0
111121
private var _minHeight: CGFloat = 0
112122
private var _previousFrame: CGRect = CGRect.zero
@@ -152,12 +162,13 @@ open class NextGrowingTextView: UIScrollView {
152162

153163
private func setup() {
154164

165+
_textView.textContainerInset = .init(top: 4, left: 0, bottom: 4, right: 0)
155166
_textView.isScrollEnabled = false
156167
_textView.font = UIFont.systemFont(ofSize: 16)
157168
_textView.backgroundColor = UIColor.clear
158169
addSubview(_textView)
159-
_minHeight = simulateHeight(1)
160-
maxNumberOfLines = 3
170+
171+
updateMinimumAndMaximumHeight()
161172

162173
_textView.didChange = { [weak self] in
163174
self?.fitToScrollView()
@@ -218,12 +229,12 @@ open class NextGrowingTextView: UIScrollView {
218229
}
219230

220231
private func scrollToBottom() {
221-
guard !disableAutomaticScrollToBottom else { return }
232+
guard !isAutomaticScrollToBottomEnabled else { return }
222233
contentOffset.y = contentSize.height - frame.height
223234
}
224235

225236
private func updateMinimumAndMaximumHeight() {
226-
_minHeight = simulateHeight(1)
237+
_minHeight = simulateHeight(minNumberOfLines)
227238
_maxHeight = simulateHeight(maxNumberOfLines)
228239
fitToScrollView()
229240
}

0 commit comments

Comments
 (0)