diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..9f55b2c --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +3.0 diff --git a/ALTextInputBar.podspec b/ALTextInputBar.podspec index 9414282..7613887 100644 --- a/ALTextInputBar.podspec +++ b/ALTextInputBar.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "ALTextInputBar" - spec.version = "1.1.0" + spec.version = "1.1.1" spec.summary = "An auto growing text input bar for messaging apps" spec.source = { :git => "https://github.com/AlexLittlejohn/ALTextInputBar.git", :tag => spec.version.to_s } spec.requires_arc = true diff --git a/ALTextInputBar.xcodeproj/project.pbxproj b/ALTextInputBar.xcodeproj/project.pbxproj index ee712a5..63ee3e3 100644 --- a/ALTextInputBar.xcodeproj/project.pbxproj +++ b/ALTextInputBar.xcodeproj/project.pbxproj @@ -178,14 +178,17 @@ attributes = { LastSwiftMigration = 0700; LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = zero; TargetAttributes = { FA61C9401AEAEC5F00920E37 = { CreatedOnToolsVersion = 6.3.1; + DevelopmentTeam = GAPQH4AM76; + LastSwiftMigration = 0800; }; FA61C9551AEAEC5F00920E37 = { CreatedOnToolsVersion = 6.3.1; + LastSwiftMigration = 0800; TestTargetID = FA61C9401AEAEC5F00920E37; }; }; @@ -277,8 +280,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -301,7 +306,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.3; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -323,8 +328,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -340,9 +347,10 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 8.3; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -353,11 +361,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + DEVELOPMENT_TEAM = GAPQH4AM76; INFOPLIST_FILE = "$(SRCROOT)/Example/Supporting Files/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "com.zero.$(PRODUCT_NAME:rfc1034identifier)"; + PRODUCT_BUNDLE_IDENTIFIER = com.zero.TextInputBar; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -367,11 +377,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + DEVELOPMENT_TEAM = GAPQH4AM76; INFOPLIST_FILE = "$(SRCROOT)/Example/Supporting Files/Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 7.0; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "com.zero.$(PRODUCT_NAME:rfc1034identifier)"; + PRODUCT_BUNDLE_IDENTIFIER = com.zero.TextInputBar; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; @@ -392,6 +404,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.zero.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ALTextInputBar.app/ALTextInputBar"; }; name = Debug; @@ -408,6 +421,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.zero.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ALTextInputBar.app/ALTextInputBar"; }; name = Release; diff --git a/ALTextInputBar.xcodeproj/xcshareddata/xcschemes/Example.xcscheme b/ALTextInputBar.xcodeproj/xcshareddata/xcschemes/Example.xcscheme index ffcd65b..086ac63 100644 --- a/ALTextInputBar.xcodeproj/xcshareddata/xcschemes/Example.xcscheme +++ b/ALTextInputBar.xcodeproj/xcshareddata/xcschemes/Example.xcscheme @@ -1,6 +1,6 @@ CGSize { - return CGSizeMake(UIViewNoIntrinsicMetric, defaultHeight) + override public var intrinsicContentSize: CGSize { + return CGSize(width: UIViewNoIntrinsicMetric, height: defaultHeight) } + + - public override func willMoveToSuperview(newSuperview: UIView?) { + public override func willMove(toSuperview newSuperview: UIView?) { removeKeyboardObserver() if let _newSuperview = newSuperview { - addKeyboardObserver(_newSuperview) + addKeyboardObserver(newSuperview: _newSuperview) } - super.willMoveToSuperview(newSuperview) + super.willMove(toSuperview: newSuperview) } - - public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer) { + + + public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if object as? NSObject == superview && keyPath == keyboardHandlingKeyPath() { - keyboardDidChangeFrame(superview!.frame) + keyboardDidChangeFrame(frame: superview!.frame) } else { - super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) + super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) } } @@ -46,7 +49,7 @@ public class ALKeyboardObservingView: UIView { } for constraint in constraints { - if constraint.firstAttribute == NSLayoutAttribute.Height && constraint.firstItem as! NSObject == self { + if constraint.firstAttribute == NSLayoutAttribute.height && constraint.firstItem as! NSObject == self { constraint.constant = height < defaultHeight ? defaultHeight : height } } @@ -62,7 +65,7 @@ public class ALKeyboardObservingView: UIView { private func addKeyboardObserver(newSuperview: UIView) { observedView = newSuperview - newSuperview.addObserver(self, forKeyPath: keyboardHandlingKeyPath(), options: NSKeyValueObservingOptions.New, context: nil) + newSuperview.addObserver(self, forKeyPath: keyboardHandlingKeyPath(), options: NSKeyValueObservingOptions.new, context: nil) } private func removeKeyboardObserver() { @@ -73,8 +76,8 @@ public class ALKeyboardObservingView: UIView { } private func keyboardDidChangeFrame(frame: CGRect) { - let userInfo = [UIKeyboardFrameEndUserInfoKey: NSValue(CGRect:frame)] - NSNotificationCenter.defaultCenter().postNotificationName(ALKeyboardFrameDidChangeNotification, object: nil, userInfo: userInfo) + let userInfo: [AnyHashable : Any] = [UIKeyboardFrameEndUserInfoKey: NSValue(cgRect:frame)] + NotificationCenter.default.post(name: NSNotification.Name(rawValue: ALKeyboardFrameDidChangeNotification), object: nil, userInfo: userInfo) } deinit { diff --git a/ALTextInputBar/ALTextInputBar.swift b/ALTextInputBar/ALTextInputBar.swift index 02c0d5e..ddf8734 100644 --- a/ALTextInputBar/ALTextInputBar.swift +++ b/ALTextInputBar/ALTextInputBar.swift @@ -16,12 +16,12 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { // If true, display a border around the text view public var showTextViewBorder = false { didSet { - textViewBorderView.hidden = !showTextViewBorder + textViewBorderView.isHidden = !showTextViewBorder } } // TextView border insets - public var textViewBorderPadding: UIEdgeInsets = UIEdgeInsetsMake(6, 8, 6, 8) + public var textViewBorderPadding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // TextView corner radius public var textViewCornerRadius: CGFloat = 4 { @@ -40,12 +40,12 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { // TextView border color public var textViewBorderColor = UIColor(white: 0.9, alpha: 1) { didSet { - textViewBorderView.layer.borderColor = textViewBorderColor.CGColor + textViewBorderView.layer.borderColor = textViewBorderColor.cgColor } } // TextView background color - public var textViewBackgroundColor = UIColor.whiteColor() { + public var textViewBackgroundColor = UIColor.white { didSet { textViewBorderView.backgroundColor = textViewBackgroundColor } @@ -120,21 +120,21 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { let _textView = ALTextView() - _textView.textContainerInset = UIEdgeInsetsMake(1, 0, 1, 0); + _textView.textContainerInset = UIEdgeInsets(top: 6, left: 8, bottom: 6, right: 8) _textView.textContainer.lineFragmentPadding = 0 _textView.maxNumberOfLines = defaultNumberOfLines() _textView.placeholder = "Type here" - _textView.placeholderColor = UIColor.lightGrayColor() + _textView.placeholderColor = UIColor.lightGray - _textView.font = UIFont.systemFontOfSize(14) - _textView.textColor = UIColor.darkGrayColor() + _textView.font = UIFont.systemFont(ofSize: 14) + _textView.textColor = UIColor.darkGray - _textView.backgroundColor = UIColor.clearColor() + _textView.backgroundColor = UIColor.clear // This changes the caret color - _textView.tintColor = UIColor.lightGrayColor() + _textView.tintColor = UIColor.lightGray return _textView }() @@ -161,17 +161,17 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { addSubview(textViewBorderView) addSubview(textView) - textViewBorderView.hidden = !showTextViewBorder + textViewBorderView.isHidden = !showTextViewBorder textView.textViewDelegate = self - backgroundColor = UIColor.groupTableViewBackgroundColor() + backgroundColor = UIColor.groupTableViewBackground } private func createBorderView() -> UIView { let borderView = UIView() borderView.backgroundColor = textViewBackgroundColor - borderView.layer.borderColor = textViewBorderColor.CGColor + borderView.layer.borderColor = textViewBorderColor.cgColor borderView.layer.borderWidth = textViewBorderWidth borderView.layer.cornerRadius = textViewCornerRadius @@ -181,8 +181,8 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { // MARK: - View positioning and layout - - override public func intrinsicContentSize() -> CGSize { - return CGSizeMake(UIViewNoIntrinsicMetric, defaultHeight) + override public var intrinsicContentSize: CGSize { + return CGSize(width: UIViewNoIntrinsicMetric, height: defaultHeight) } override public func layoutSubviews() { @@ -191,8 +191,8 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { let size = frame.size let height = floor(size.height) - var leftViewSize = CGSizeZero - var rightViewSize = CGSizeZero + var leftViewSize = CGSize.zero + var rightViewSize = CGSize.zero if let view = leftView { leftViewSize = view.bounds.size @@ -202,7 +202,7 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { let leftViewY: CGFloat = height - (leftViewSize.height + leftViewVerticalPadding) UIView.performWithoutAnimation { - view.frame = CGRectMake(leftViewX, leftViewY, leftViewSize.width, leftViewSize.height) + view.frame = CGRect(x: leftViewX, y: leftViewY, width: leftViewSize.width, height: leftViewSize.height) } } @@ -217,7 +217,7 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { rightViewX -= (rightViewSize.width + horizontalPadding) } - view.frame = CGRectMake(rightViewX, rightViewY, rightViewSize.width, rightViewSize.height) + view.frame = CGRect(x: rightViewX, y: rightViewY, width: rightViewSize.width, height: rightViewSize.height) } let textViewPadding = (defaultHeight - textView.minimumHeight) / 2 @@ -242,7 +242,7 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { } - textView.frame = CGRectMake(textViewX, textViewY, textViewWidth, textViewHeight) + textView.frame = CGRect(x: textViewX, y: textViewY, width: textViewWidth, height: textViewHeight) let offset = UIEdgeInsetsMake(-textViewBorderPadding.top, -textViewBorderPadding.left, -textViewBorderPadding.bottom, -textViewBorderPadding.right) textViewBorderView.frame = UIEdgeInsetsInsetRect(textView.frame, offset) @@ -250,7 +250,7 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { public func updateViews(animated: Bool) { if animated { - UIView.animateWithDuration(0.2) { + UIView.animate(withDuration: 0.2) { self.setNeedsLayout() self.layoutIfNeeded() } @@ -269,7 +269,7 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { let height = padding + newHeight for constraint in constraints { - if constraint.firstAttribute == NSLayoutAttribute.Height && constraint.firstItem as! NSObject == self { + if constraint.firstAttribute == NSLayoutAttribute.height && constraint.firstItem as! NSObject == self { constraint.constant = height < defaultHeight ? defaultHeight : height } } @@ -277,69 +277,71 @@ public class ALTextInputBar: UIView, ALTextViewDelegate { frame.size.height = height if let ko = keyboardObserver { - ko.updateHeight(height) + ko.updateHeight(height: height) } - if let d = delegate, m = d.inputBarDidChangeHeight { + if let d = delegate, let m = d.inputBarDidChangeHeight { m(height) } + + textView.frame.size.height = newHeight } - public final func textViewDidChange(textView: UITextView) { + public final func textViewDidChange(_ textView: UITextView) { self.textView.textViewDidChange() - - let shouldShowButton = textView.text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 + + let shouldShowButton = textView.text.lengthOfBytes(using: String.Encoding.utf8) > 0 if showRightButton != shouldShowButton && !alwaysShowRightButton { showRightButton = shouldShowButton - updateViews(true) + updateViews(animated: true) } - if let d = delegate, m = d.textViewDidChange { + if let d = delegate, let m = d.textViewDidChange { m(self.textView) } } - public func textViewShouldBeginEditing(textView: UITextView) -> Bool { + public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool { var beginEditing: Bool = true - if let d = delegate, m = d.textViewShouldEndEditing { + if let d = delegate, let m = d.textViewShouldEndEditing { beginEditing = m(self.textView) } return beginEditing } - public func textViewShouldEndEditing(textView: UITextView) -> Bool { + public func textViewShouldEndEditing(_ textView: UITextView) -> Bool { var endEditing = true - if let d = delegate, m = d.textViewShouldEndEditing { + if let d = delegate, let m = d.textViewShouldEndEditing { endEditing = m(self.textView) } return endEditing } - public func textViewDidBeginEditing(textView: UITextView) { - if let d = delegate, m = d.textViewDidBeginEditing { + public func textViewDidBeginEditing(_ textView: UITextView) { + if let d = delegate, let m = d.textViewDidBeginEditing { m(self.textView) } } - public func textViewDidEndEditing(textView: UITextView) { - if let d = delegate, m = d.textViewDidEndEditing { + public func textViewDidEndEditing(_ textView: UITextView) { + if let d = delegate, let m = d.textViewDidEndEditing { m(self.textView) } } - public func textViewDidChangeSelection(textView: UITextView) { - if let d = delegate, m = d.textViewDidChangeSelection { + public func textViewDidChangeSelection(_ textView: UITextView) { + if let d = delegate, let m = d.textViewDidChangeSelection { m(self.textView) } } - public func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { + public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { var shouldChange = true - if let d = delegate, m = d.textView { - shouldChange = m(self.textView, shouldChangeTextInRange: range, replacementText: text) + if let d = delegate, let m = d.textView { + shouldChange = m(self.textView, range, text) } return shouldChange } diff --git a/ALTextInputBar/ALTextInputBarDelegate.swift b/ALTextInputBar/ALTextInputBarDelegate.swift index 37fc878..9dfb070 100644 --- a/ALTextInputBar/ALTextInputBarDelegate.swift +++ b/ALTextInputBar/ALTextInputBarDelegate.swift @@ -10,16 +10,16 @@ import UIKit @objc public protocol ALTextInputBarDelegate: NSObjectProtocol { - optional func textViewShouldBeginEditing(textView: ALTextView) -> Bool - optional func textViewShouldEndEditing(textView: ALTextView) -> Bool + @objc optional func textViewShouldBeginEditing(textView: ALTextView) -> Bool + @objc optional func textViewShouldEndEditing(textView: ALTextView) -> Bool - optional func textViewDidBeginEditing(textView: ALTextView) - optional func textViewDidEndEditing(textView: ALTextView) + @objc optional func textViewDidBeginEditing(textView: ALTextView) + @objc optional func textViewDidEndEditing(textView: ALTextView) - optional func textViewDidChange(textView: ALTextView) - optional func textViewDidChangeSelection(textView: ALTextView) + @objc optional func textViewDidChange(textView: ALTextView) + @objc optional func textViewDidChangeSelection(textView: ALTextView) - optional func textView(textView: ALTextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool + @objc optional func textView(textView: ALTextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool - optional func inputBarDidChangeHeight(height: CGFloat) + @objc optional func inputBarDidChangeHeight(height: CGFloat) } diff --git a/ALTextInputBar/ALTextInputUtilities.swift b/ALTextInputBar/ALTextInputUtilities.swift index 32d8706..d94a13d 100644 --- a/ALTextInputBar/ALTextInputUtilities.swift +++ b/ALTextInputBar/ALTextInputUtilities.swift @@ -21,18 +21,18 @@ internal func defaultNumberOfLines() -> CGFloat { internal extension UIDevice { internal class func isIPad() -> Bool { - return UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad + return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad } internal class func isIPhone() -> Bool { - return UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone + return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone } internal class func isIPhone4() -> Bool { - return UIDevice.isIPhone() && UIScreen.mainScreen().bounds.size.height < 568.0 + return UIDevice.isIPhone() && UIScreen.main.bounds.size.height < 568.0 } internal class func floatVersion() -> Float { - return (UIDevice.currentDevice().systemVersion as NSString).floatValue + return (UIDevice.current.systemVersion as NSString).floatValue } } diff --git a/ALTextInputBar/ALTextView.swift b/ALTextInputBar/ALTextView.swift index 93e439d..a638342 100644 --- a/ALTextInputBar/ALTextView.swift +++ b/ALTextInputBar/ALTextView.swift @@ -67,15 +67,25 @@ public class ALTextView: UITextView { _placeholderLabel.autoresizesSubviews = false _placeholderLabel.numberOfLines = 1 _placeholderLabel.font = self.font - _placeholderLabel.backgroundColor = UIColor.clearColor() + _placeholderLabel.backgroundColor = UIColor.clear _placeholderLabel.textColor = self.tintColor - _placeholderLabel.hidden = true + _placeholderLabel.isHidden = true self.addSubview(_placeholderLabel) return _placeholderLabel - }() - + }() + + public override var textAlignment: NSTextAlignment { + get { + return super.textAlignment + } + set { + super.textAlignment = newValue + placeholderLabel.textAlignment = newValue + } + } + /// The maximum number of lines that will be shown before the text view will scroll. 0 = no limit public var maxNumberOfLines: CGFloat = 0 public var expectedHeight: CGFloat = 0 @@ -96,17 +106,16 @@ public class ALTextView: UITextView { } private func commonInit() { - scrollEnabled = false -// NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewDidChange:", name:UITextViewTextDidChangeNotification, object: self) + isScrollEnabled = false } override public func layoutSubviews() { super.layoutSubviews() - placeholderLabel.hidden = shouldHidePlaceholder() - if !placeholderLabel.hidden { - placeholderLabel.frame = placeholderRectThatFits(bounds) - sendSubviewToBack(placeholderLabel) + placeholderLabel.isHidden = shouldHidePlaceholder() + if !placeholderLabel.isHidden { + placeholderLabel.frame = placeholderRectThatFits(rect: bounds) + sendSubview(toBack: placeholderLabel) } } @@ -116,7 +125,7 @@ public class ALTextView: UITextView { */ private func updateSize() { - var maxHeight = CGFloat.max + var maxHeight = CGFloat.greatestFiniteMagnitude if maxNumberOfLines > 0 { maxHeight = (ceil(font!.lineHeight) * maxNumberOfLines) + textContainerInset.top + textContainerInset.bottom @@ -126,14 +135,14 @@ public class ALTextView: UITextView { if roundedHeight >= maxHeight { expectedHeight = maxHeight - scrollEnabled = true + isScrollEnabled = true } else { expectedHeight = roundedHeight - scrollEnabled = false + isScrollEnabled = false } if textViewDelegate != nil { - textViewDelegate?.textViewHeightChanged(self, newHeight:expectedHeight) + textViewDelegate?.textViewHeightChanged(textView: self, newHeight:expectedHeight) } ensureCaretDisplaysCorrectly() @@ -147,10 +156,14 @@ public class ALTextView: UITextView { if let font = font { let attributes = [NSFontAttributeName: font] - let boundingSize = CGSizeMake(frame.size.width, CGFloat.max) - let size = (text as NSString).boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil) + let boundingSize = CGSize(width: frame.size.width, height: CGFloat.greatestFiniteMagnitude) + let size = text.boundingRect(with: boundingSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil) newHeight = ceil(size.height) } + + if let font = font, newHeight < font.lineHeight { + newHeight = font.lineHeight + } return newHeight + textContainerInset.top + textContainerInset.bottom } @@ -160,7 +173,7 @@ public class ALTextView: UITextView { */ private func ensureCaretDisplaysCorrectly() { if let s = selectedTextRange { - let rect = caretRectForPosition(s.end) + let rect = caretRect(for: s.end) UIView.performWithoutAnimation({ () -> Void in self.scrollRectToVisible(rect, animated: false) }) @@ -175,7 +188,7 @@ public class ALTextView: UITextView { - returns: true if it should not be visible */ private func shouldHidePlaceholder() -> Bool { - return placeholder.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 || text.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 + return placeholder.lengthOfBytes(using: String.Encoding.utf8) == 0 || text.lengthOfBytes(using: String.Encoding.utf8) > 0 } /** @@ -185,21 +198,18 @@ public class ALTextView: UITextView { - returns: The placeholder label frame */ private func placeholderRectThatFits(rect: CGRect) -> CGRect { - - var placeholderRect = CGRectZero - placeholderRect.size = placeholderLabel.sizeThatFits(rect.size) - placeholderRect.origin = UIEdgeInsetsInsetRect(rect, textContainerInset).origin - let padding = textContainer.lineFragmentPadding - placeholderRect.origin.x += padding - - return placeholderRect + var placeHolderRect = UIEdgeInsetsInsetRect(rect, textContainerInset) + placeHolderRect.origin.x += padding + placeHolderRect.size.width -= padding * 2 + + return placeHolderRect } - + //MARK: - Notifications - internal func textViewDidChange() { - placeholderLabel.hidden = shouldHidePlaceholder() + placeholderLabel.isHidden = shouldHidePlaceholder() updateSize() } } diff --git a/ALTextInputBarTests/ALTextInputBarTests.swift b/ALTextInputBarTests/ALTextInputBarTests.swift index 6618763..c292557 100644 --- a/ALTextInputBarTests/ALTextInputBarTests.swift +++ b/ALTextInputBarTests/ALTextInputBarTests.swift @@ -28,7 +28,7 @@ class ALTextInputBarTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } } diff --git a/Example/AppDelegate.swift b/Example/AppDelegate.swift index 536eb29..6e13e6f 100644 --- a/Example/AppDelegate.swift +++ b/Example/AppDelegate.swift @@ -13,10 +13,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { + // Override point for customization after application launch. - - window = UIWindow(frame: UIScreen.mainScreen().bounds) + + window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = ViewController() window?.makeKeyAndVisible()