Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions B68UIFloatLabelTextField/B68UIFloatLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

import UIKit

public class B68UIFloatLabelTextField: UITextField {
open class B68UIFloatLabelTextField: UITextField {

/**
The floating label that is displayed above the text field when there is other
text in the text field.
*/
public var floatingLabel = UILabel(frame: CGRectMake(0, 0, 0, 0))
open var floatingLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

/**
The color of the floating label displayed above the text field when it is in
an active state (i.e. the associated text view is first responder).

@discussion Note: Default Color is blue.
*/
@IBInspectable public var activeTextColorfloatingLabel : UIColor = UIColor.blueColor() {
@IBInspectable open var activeTextColorfloatingLabel : UIColor = UIColor.blue {
didSet {
floatingLabel.textColor = activeTextColorfloatingLabel
}
Expand All @@ -33,7 +33,7 @@ public class B68UIFloatLabelTextField: UITextField {

@discussion Note: 70% gray is used by default if this is nil.
*/
@IBInspectable public var inactiveTextColorfloatingLabel : UIColor = UIColor(white: 0.7, alpha: 1.0) {
@IBInspectable open var inactiveTextColorfloatingLabel : UIColor = UIColor(white: 0.7, alpha: 1.0) {
didSet {
floatingLabel.textColor = inactiveTextColorfloatingLabel
}
Expand All @@ -42,27 +42,27 @@ public class B68UIFloatLabelTextField: UITextField {
/**
The default dynamic test size
*/
public var placeHolderTextSize : String = UIFontTextStyleCaption2 {
open var placeHolderTextSize : String = UIFontTextStyle.caption2.rawValue {
didSet {
floatingLabel.font = UIFont.preferredFontForTextStyle(placeHolderTextSize)
floatingLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: placeHolderTextSize))
}
}

/**
Used to cache the placeholder string.
*/
private var cachedPlaceholder = NSString()
fileprivate var cachedPlaceholder = NSString()

/**
Used to draw the placeholder string if necessary. Starting value is true.
*/
private var shouldDrawPlaceholder = true
fileprivate var shouldDrawPlaceholder = true

/**
default padding for floatingLabel
*/
public var verticalPadding : CGFloat = 0
public var horizontalPadding : CGFloat = 0
open var verticalPadding : CGFloat = 0
open var horizontalPadding : CGFloat = 0


//MARK: Initializer
Expand All @@ -87,48 +87,48 @@ public class B68UIFloatLabelTextField: UITextField {
//MARK: Deinit
deinit {
// remove observer
NSNotificationCenter.defaultCenter().removeObserver(self)
NotificationCenter.default.removeObserver(self)
}


//MARK: Setter & Getter
override public var placeholder : String? {
override open var placeholder : String? {
get {
return super.placeholder
}
set (newValue) {
super.placeholder = newValue
if (cachedPlaceholder != newValue) {
cachedPlaceholder = newValue!
if (cachedPlaceholder as String != newValue) {
cachedPlaceholder = newValue! as NSString
floatingLabel.text = self.cachedPlaceholder as String
floatingLabel.sizeToFit()
}
}
}

override public func hasText() ->Bool {
override open var hasText :Bool {
return !text!.isEmpty
}

//MARK: Setup
private func setup() {
fileprivate func setup() {
setupObservers()
setupFloatingLabel()
applyFonts()
setupViewDefaults()
}

private func setupObservers() {
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidChange), name: UITextFieldTextDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(B68UIFloatLabelTextField.fontSizeDidChange), name: UIContentSizeCategoryDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidBeginEditing), name: UITextFieldTextDidBeginEditingNotification, object: self)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidEndEditing), name: UITextFieldTextDidEndEditingNotification, object: self)
fileprivate func setupObservers() {
NotificationCenter.default.addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidChange), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(B68UIFloatLabelTextField.fontSizeDidChange), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidBeginEditing), name: NSNotification.Name.UITextFieldTextDidBeginEditing, object: self)
NotificationCenter.default.addObserver(self, selector:#selector(B68UIFloatLabelTextField.textFieldTextDidEndEditing), name: NSNotification.Name.UITextFieldTextDidEndEditing, object: self)
}

private func setupFloatingLabel() {
fileprivate func setupFloatingLabel() {
// Create the floating label instance and add it to the view
floatingLabel.alpha = 1
floatingLabel.center = CGPointMake(horizontalPadding, verticalPadding)
floatingLabel.center = CGPoint(x: horizontalPadding, y: verticalPadding)
addSubview(floatingLabel)
//TODO: Set tint color instead of default value

Expand All @@ -138,16 +138,16 @@ public class B68UIFloatLabelTextField: UITextField {

}

private func applyFonts() {
fileprivate func applyFonts() {

// set floatingLabel to have the same font as the textfield
floatingLabel.font = UIFont(name: font!.fontName, size: UIFont.preferredFontForTextStyle(placeHolderTextSize).pointSize)
floatingLabel.font = UIFont(name: font!.fontName, size: UIFont.preferredFont(forTextStyle: UIFontTextStyle(rawValue: placeHolderTextSize)).pointSize)
}

private func setupViewDefaults() {
fileprivate func setupViewDefaults() {

// set vertical padding
verticalPadding = 0.5 * CGRectGetHeight(self.frame)
verticalPadding = 0.5 * self.frame.height

// make sure the placeholder setter methods are called
if let ph = placeholder {
Expand All @@ -158,26 +158,26 @@ public class B68UIFloatLabelTextField: UITextField {
}

//MARK: - Drawing & Animations
override public func layoutSubviews() {
override open func layoutSubviews() {
super.layoutSubviews()
if (isFirstResponder() && !hasText()) {
if (isFirstResponder && !hasText) {
hideFloatingLabel()
} else if(hasText()) {
} else if(hasText) {
showFloatingLabelWithAnimation(true)
}
}

func showFloatingLabelWithAnimation(isAnimated : Bool)
func showFloatingLabelWithAnimation(_ isAnimated : Bool)
{
let fl_frame = CGRectMake(
horizontalPadding,
0,
CGRectGetWidth(self.floatingLabel.frame),
CGRectGetHeight(self.floatingLabel.frame)
let fl_frame = CGRect(
x: horizontalPadding,
y: 0,
width: self.floatingLabel.frame.width,
height: self.floatingLabel.frame.height
)
if (isAnimated) {
let options: UIViewAnimationOptions = [UIViewAnimationOptions.BeginFromCurrentState, UIViewAnimationOptions.CurveEaseOut]
UIView.animateWithDuration(0.2, delay: 0, options: options, animations: {
let options: UIViewAnimationOptions = [UIViewAnimationOptions.beginFromCurrentState, UIViewAnimationOptions.curveEaseOut]
UIView.animate(withDuration: 0.2, delay: 0, options: options, animations: {
self.floatingLabel.alpha = 1
self.floatingLabel.frame = fl_frame
}, completion: nil)
Expand All @@ -188,14 +188,14 @@ public class B68UIFloatLabelTextField: UITextField {
}

func hideFloatingLabel () {
let fl_frame = CGRectMake(
horizontalPadding,
verticalPadding,
CGRectGetWidth(self.floatingLabel.frame),
CGRectGetHeight(self.floatingLabel.frame)
let fl_frame = CGRect(
x: horizontalPadding,
y: verticalPadding,
width: self.floatingLabel.frame.width,
height: self.floatingLabel.frame.height
)
let options: UIViewAnimationOptions = [UIViewAnimationOptions.BeginFromCurrentState, UIViewAnimationOptions.CurveEaseIn]
UIView.animateWithDuration(0.2, delay: 0, options: options, animations: {
let options: UIViewAnimationOptions = [UIViewAnimationOptions.beginFromCurrentState, UIViewAnimationOptions.curveEaseIn]
UIView.animate(withDuration: 0.2, delay: 0, options: options, animations: {
self.floatingLabel.alpha = 0
self.floatingLabel.frame = fl_frame
}, completion: nil
Expand All @@ -204,24 +204,24 @@ public class B68UIFloatLabelTextField: UITextField {


//MARK: - Auto Layout
override public func intrinsicContentSize() -> CGSize {
override open var intrinsicContentSize : CGSize {
return sizeThatFits(frame.size)
}

// Adds padding so these text fields align with B68FloatingPlaceholderTextView's
override public func textRectForBounds (bounds :CGRect) -> CGRect
override open func textRect (forBounds bounds :CGRect) -> CGRect
{
return UIEdgeInsetsInsetRect(super.textRectForBounds(bounds), floatingLabelInsets())
return UIEdgeInsetsInsetRect(super.textRect(forBounds: bounds), floatingLabelInsets())
}

// Adds padding so these text fields align with B68FloatingPlaceholderTextView's
override public func editingRectForBounds (bounds : CGRect) ->CGRect
override open func editingRect (forBounds bounds : CGRect) ->CGRect
{
return UIEdgeInsetsInsetRect(super.editingRectForBounds(bounds), floatingLabelInsets())
return UIEdgeInsetsInsetRect(super.editingRect(forBounds: bounds), floatingLabelInsets())
}

//MARK: - Helpers
private func floatingLabelInsets() -> UIEdgeInsets {
fileprivate func floatingLabelInsets() -> UIEdgeInsets {
floatingLabel.sizeToFit()
return UIEdgeInsetsMake(
floatingLabel.font.lineHeight,
Expand All @@ -232,9 +232,9 @@ public class B68UIFloatLabelTextField: UITextField {


//MARK: - Observers
func textFieldTextDidChange(notification : NSNotification) {
func textFieldTextDidChange(_ notification : Notification) {
let previousShouldDrawPlaceholderValue = shouldDrawPlaceholder
shouldDrawPlaceholder = !hasText()
shouldDrawPlaceholder = !hasText

// Only redraw if self.shouldDrawPlaceholder value was changed
if (previousShouldDrawPlaceholderValue != shouldDrawPlaceholder) {
Expand All @@ -246,18 +246,18 @@ public class B68UIFloatLabelTextField: UITextField {
}
}
//MARK: TextField Editing Observer
func textFieldTextDidEndEditing(notification : NSNotification) {
if (hasText()) {
func textFieldTextDidEndEditing(_ notification : Notification) {
if (hasText) {
floatingLabel.textColor = inactiveTextColorfloatingLabel
}
}

func textFieldTextDidBeginEditing(notification : NSNotification) {
func textFieldTextDidBeginEditing(_ notification : Notification) {
floatingLabel.textColor = activeTextColorfloatingLabel
}

//MARK: Font Size Change Oberver
func fontSizeDidChange (notification : NSNotification) {
func fontSizeDidChange (_ notification : Notification) {
applyFonts()
invalidateIntrinsicContentSize()
setNeedsLayout()
Expand Down
3 changes: 3 additions & 0 deletions Example/UIFloatLabelTextInput.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
43C4391D19BCF9CE0017D531 = {
CreatedOnToolsVersion = 6.0;
DevelopmentTeam = YV6CG57Z4A;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -276,6 +277,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.base68.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -291,6 +293,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.base68.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
12 changes: 6 additions & 6 deletions Example/UIFloatLabelTextInput/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
Loading