Skip to content

Commit

Permalink
Merge pull request #49 from muukii/beta
Browse files Browse the repository at this point in the history
You released from verbose wrapper API
  • Loading branch information
muukii authored May 1, 2017
2 parents b3b823c + e8e8d73 commit aeb1873
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 458 deletions.
6 changes: 3 additions & 3 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class ViewController: UIViewController {

self.growingTextView.layer.cornerRadius = 4
self.growingTextView.backgroundColor = UIColor(white: 0.9, alpha: 1)
self.growingTextView.textContainerInset = UIEdgeInsets(top: 16, left: 0, bottom: 4, right: 0)
self.growingTextView.textView.textContainerInset = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
self.growingTextView.placeholderAttributedText = NSAttributedString(string: "Placeholder text",
attributes: [NSFontAttributeName: self.growingTextView.font!,
attributes: [NSFontAttributeName: self.growingTextView.textView.font!,
NSForegroundColorAttributeName: UIColor.gray
]
)
Expand All @@ -58,7 +58,7 @@ class ViewController: UIViewController {


@IBAction func handleSendButton(_ sender: AnyObject) {
self.growingTextView.text = ""
self.growingTextView.textView.text = ""
self.view.endEditing(true)
}

Expand Down
4 changes: 2 additions & 2 deletions NextGrowingTextView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "NextGrowingTextView"
s.version = "0.8.5"
s.summary = "The next in the generations of 'growing textviews' optimized for iOS 7 and above."
s.version = "1.0.0"
s.summary = "The next in the generations of 'growing textviews' optimized for iOS 8 and above."
s.homepage = "https://github.com/muukii/NextGrowingTextView"
s.license = 'MIT'
s.author = { "muukii" => "[email protected]" }
Expand Down
4 changes: 3 additions & 1 deletion NextGrowingTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
4BDB87E81DC0423F00E70D5B /* NextGrowingTextView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NextGrowingTextView.h; sourceTree = "<group>"; };
4BDB87E91DC0423F00E70D5B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4BDB87F01DC0425600E70D5B /* NextGrowingInternalTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NextGrowingInternalTextView.swift; sourceTree = "<group>"; };
4BDB87F11DC0425600E70D5B /* NextGrowingTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NextGrowingTextView.swift; sourceTree = "<group>"; };
4BDB87F11DC0425600E70D5B /* NextGrowingTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = NextGrowingTextView.swift; sourceTree = "<group>"; tabWidth = 2; };
4BDB87F81DC0426A00E70D5B /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4BDB87FA1DC0426A00E70D5B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4BDB87FC1DC0426A00E70D5B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -462,6 +462,7 @@
4BDB87EF1DC0423F00E70D5B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4BDB88071DC0426A00E70D5B /* Build configuration list for PBXNativeTarget "Demo" */ = {
isa = XCConfigurationList;
Expand All @@ -470,6 +471,7 @@
4BDB88091DC0426A00E70D5B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
140 changes: 73 additions & 67 deletions NextGrowingTextView/NextGrowingInternalTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,82 @@ import UIKit
// MARK: - NextGrowingInternalTextView: UITextView

internal class NextGrowingInternalTextView: UITextView {

// MARK: - Internal

override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)

NotificationCenter.default.addObserver(self, selector: #selector(NextGrowingInternalTextView.textDidChangeNotification(_ :)), name: NSNotification.Name.UITextViewTextDidChange, object: self)
}

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

deinit {
NotificationCenter.default.removeObserver(self)
}

override var text: String! {
didSet {
self.updatePlaceholder()
}
}

var placeholderAttributedText: NSAttributedString? {
didSet {
self.setNeedsDisplay()
}
}

override func layoutSubviews() {
super.layoutSubviews()
self.setNeedsDisplay()
}

override func draw(_ rect: CGRect) {

super.draw(rect)

guard self.displayPlaceholder == true else {
return
}

let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = self.textAlignment

let targetRect = CGRect(x: 5 + self.textContainerInset.left,
y: self.textContainerInset.top,
width: self.frame.size.width - (self.textContainerInset.left + self.textContainerInset.right),
height: self.frame.size.height - (self.textContainerInset.top + self.textContainerInset.bottom))

let attributedString = self.placeholderAttributedText
attributedString?.draw(in: targetRect)

// MARK: - Internal

var didChange: () -> Void = {}

override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)

NotificationCenter.default.addObserver(self, selector: #selector(NextGrowingInternalTextView.textDidChangeNotification(_ :)), name: NSNotification.Name.UITextViewTextDidChange, object: self)
}

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

deinit {
NotificationCenter.default.removeObserver(self)
}

override var text: String! {
didSet {
didChange()
updatePlaceholder()
}

// MARK: Private

fileprivate var displayPlaceholder: Bool = true {
didSet {
if oldValue != self.displayPlaceholder {
self.setNeedsDisplay()
}
}
}

var placeholderAttributedText: NSAttributedString? {
didSet {
setNeedsDisplay()
}

fileprivate dynamic func textDidChangeNotification(_ notification: Notification) {

self.updatePlaceholder()
}

override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}

override func draw(_ rect: CGRect) {

super.draw(rect)

guard displayPlaceholder == true else {
return
}

let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment

let targetRect = CGRect(
x: 5 + textContainerInset.left,
y: textContainerInset.top,
width: frame.size.width - (textContainerInset.left + textContainerInset.right),
height: frame.size.height - (textContainerInset.top + textContainerInset.bottom)
)

fileprivate func updatePlaceholder() {
self.displayPlaceholder = self.text.characters.count == 0
let attributedString = placeholderAttributedText
attributedString?.draw(in: targetRect)
}

// MARK: Private

private var displayPlaceholder: Bool = true {
didSet {
if oldValue != displayPlaceholder {
setNeedsDisplay()
}
}
}

private dynamic func textDidChangeNotification(_ notification: Notification) {

updatePlaceholder()
didChange()
}

private func updatePlaceholder() {
displayPlaceholder = text.characters.count == 0
}
}
Loading

0 comments on commit aeb1873

Please sign in to comment.