diff --git a/Readme.markdown b/Readme.markdown index 7fd13bb..815ff91 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -2,10 +2,11 @@ Handy UITextField additions like insetting text and other elements. -SAMTextField is tested on iOS 6-8 and requires ARC. Released under the [MIT license](LICENSE). +SAMTextField is tested on iOS 7-9 and requires ARC. Released under the [MIT license](LICENSE). ## Usage +#### Objective-C ``` objc // Initialize a text field SAMTextField *textField = [[SAMTextField alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 44.0f)]; @@ -14,8 +15,17 @@ SAMTextField *textField = [[SAMTextField alloc] initWithFrame:CGRectMake(20.0f, textField.textEdgeInsets = UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f); ``` -See the [header](SAMTextField/SAMTextField.h) for full documentation. +#### Swift +``` swift +// Initialize a text field +let textField = SAMTextField(frame:CGRectMake(20.0, 20.0, 280.0, 44.0)) + +// Inset some text +textField.textEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) +``` + +See the [header](SAMTextField/SAMTextField.swift) for full documentation. ## Installation -Simply add the files in the `SAMTextField.h` and `SAMTextField.m` to your project or add `SAMTextField` to your Podfile if you're using CocoaPods. +Simply add the file `SAMTextField.swift` to your project or add `SAMTextField` to your Podfile if you're using CocoaPods. diff --git a/SAMTextField/SAMTextField.h b/SAMTextField/SAMTextField.h deleted file mode 100644 index d51be2e..0000000 --- a/SAMTextField/SAMTextField.h +++ /dev/null @@ -1,66 +0,0 @@ -// -// SAMTextField.h -// SAMTextField -// -// Created by Sam Soffes on 3/11/10. -// Copyright 2010-2014 Sam Soffes. All rights reserved. -// - -/** - Simple UITextField subclass to adds text insets. - */ -@interface SAMTextField : UITextField - -///------------------------------ -/// @name Drawing and Positioning -///------------------------------ - -/** - The inset or outset margins for the edges of the text content drawing rectangle. - - Use this property to resize and reposition the effective drawing rectangle for the text content. You can specify a - different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that - edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the - `UIEdgeInsetsMake` function to construct a value for this property. - - The default value is `UIEdgeInsetsZero`. - */ -@property (nonatomic) UIEdgeInsets textEdgeInsets; - -/** - The inset or outset margins for the edges of the clear button drawing rectangle. - - Use this property to resize and reposition the effective drawing rectangle for the clear button content. You can - specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are - respected. A positive value will move the clear button farther away from the top right corner. Use the - `UIEdgeInsetsMake` function to construct a value for this property. - - The default value is `UIEdgeInsetsZero`. - */ -@property (nonatomic) UIEdgeInsets clearButtonEdgeInsets; - -/** - The inset or outset margins for the edges of the view assigned to `rightView`. - - Use this property to resize and reposition the effective drawing rectangle for the right view content. You can - specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are - respected. A positive value will move the view farther away from the top right corner. Use the - `UIEdgeInsetsMake` function to construct a value for this property. - - The default value is `UIEdgeInsetsZero`. - */ -@property (nonatomic) UIEdgeInsets rightViewInsets; - -/** - The inset or outset margins for the edges of the view assigned to `leftView`. - - Use this property to resize and reposition the effective drawing rectangle for the left view content. You can - specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are - respected. A positive value will move the view farther away from the top right corner. Use the - `UIEdgeInsetsMake` function to construct a value for this property. - - The default value is `UIEdgeInsetsZero`. - */ -@property (nonatomic) UIEdgeInsets leftViewInsets; - -@end diff --git a/SAMTextField/SAMTextField.m b/SAMTextField/SAMTextField.m deleted file mode 100644 index d57aa23..0000000 --- a/SAMTextField/SAMTextField.m +++ /dev/null @@ -1,81 +0,0 @@ -// -// SAMTextField.m -// SAMTextField -// -// Created by Sam Soffes on 3/11/10. -// Copyright 2010-2014 Sam Soffes. All rights reserved. -// - -#import "SAMTextField.h" - -@implementation SAMTextField - -#pragma mark - UIView - -- (id)initWithCoder:(NSCoder *)aDecoder { - if ((self = [super initWithCoder:aDecoder])) { - [self initialize]; - } - return self; -} - - -- (id)initWithFrame:(CGRect)frame { - if ((self = [super initWithFrame:frame])) { - [self initialize]; - } - return self; -} - - -#pragma mark - UITextField - -- (CGRect)textRectForBounds:(CGRect)bounds { - return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], self.textEdgeInsets); -} - - -- (CGRect)editingRectForBounds:(CGRect)bounds { - return [self textRectForBounds:bounds]; -} - - -- (CGRect)placeholderRectForBounds:(CGRect)bounds { - return [self textRectForBounds:bounds]; -} - - -- (CGRect)clearButtonRectForBounds:(CGRect)bounds { - CGRect rect = [super clearButtonRectForBounds:bounds]; - rect.origin.x += self.clearButtonEdgeInsets.right; - rect.origin.y += self.clearButtonEdgeInsets.top; - return rect; -} - - -- (CGRect)rightViewRectForBounds:(CGRect)bounds { - CGRect rect = [super rightViewRectForBounds:bounds]; - rect.origin.x += self.rightViewInsets.right; - rect.origin.y += self.rightViewInsets.top; - return rect; -} - - -- (CGRect)leftViewRectForBounds:(CGRect)bounds { - CGRect rect = [super leftViewRectForBounds:bounds]; - rect.origin.x += self.leftViewInsets.left; - rect.origin.y += self.leftViewInsets.top; - return rect; -} - - -#pragma mark - Private - -- (void)initialize { - self.textEdgeInsets = UIEdgeInsetsZero; - self.clearButtonEdgeInsets = UIEdgeInsetsZero; - self.leftViewInsets = UIEdgeInsetsZero; - self.rightViewInsets = UIEdgeInsetsZero; -} - -@end diff --git a/SAMTextField/SAMTextField.swift b/SAMTextField/SAMTextField.swift new file mode 100755 index 0000000..d13a285 --- /dev/null +++ b/SAMTextField/SAMTextField.swift @@ -0,0 +1,103 @@ +// +// SAMTextField.swift +// SAMTextField +// +// Created by Sam Soffes on 3/11/10. +// Copyright 2010-2014 Sam Soffes. All rights reserved. +// + +import UIKit + +/** +Simple UITextField subclass to adds text insets. +*/ +public class SAMTextField : UITextField { + + ///------------------------------ + /// @name Drawing and Positioning + ///------------------------------ + + /** + The inset or outset margins for the edges of the text content drawing rectangle. + + Use this property to resize and reposition the effective drawing rectangle for the text content. You can specify a + different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that + edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the + `UIEdgeInsetsMake` function to construct a value for this property. + + The default value is `UIEdgeInsetsZero`. + */ + public var textEdgeInsets = UIEdgeInsetsZero + + /** + The inset or outset margins for the edges of the clear button drawing rectangle. + + Use this property to resize and reposition the effective drawing rectangle for the clear button content. You can + specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are + respected. A positive value will move the clear button farther away from the top right corner. Use the + `UIEdgeInsetsMake` function to construct a value for this property. + + The default value is `UIEdgeInsetsZero`. + */ + public var clearButtonEdgeInsets = UIEdgeInsetsZero + + /** + The inset or outset margins for the edges of the view assigned to `rightView`. + + Use this property to resize and reposition the effective drawing rectangle for the right view content. You can + specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are + respected. A positive value will move the view farther away from the top right corner. Use the + `UIEdgeInsetsMake` function to construct a value for this property. + + The default value is `UIEdgeInsetsZero`. + */ + public var rightViewInsets = UIEdgeInsetsZero + + /** + The inset or outset margins for the edges of the view assigned to `leftView`. + + Use this property to resize and reposition the effective drawing rectangle for the left view content. You can + specify a different value for each of the four insets (top, left, bottom, right), but only the top and right insets are + respected. A positive value will move the view farther away from the top right corner. Use the + `UIEdgeInsetsMake` function to construct a value for this property. + + The default value is `UIEdgeInsetsZero`. + */ + public var leftViewInsets = UIEdgeInsetsZero + + + // MARK: - UITextField + + public override func textRectForBounds(bounds: CGRect) -> CGRect { + return UIEdgeInsetsInsetRect(super.textRectForBounds(bounds), textEdgeInsets) + } + + public override func editingRectForBounds(bounds: CGRect) -> CGRect { + return textRectForBounds(bounds) + } + + public override func placeholderRectForBounds(bounds: CGRect) -> CGRect { + return textRectForBounds(bounds) + } + + public override func clearButtonRectForBounds(bounds: CGRect) -> CGRect { + var rect = super.clearButtonRectForBounds(bounds) + rect.origin.x += clearButtonEdgeInsets.right + rect.origin.y += clearButtonEdgeInsets.top + return rect + } + + public override func rightViewRectForBounds(bounds: CGRect) -> CGRect { + var rect = super.rightViewRectForBounds(bounds) + rect.origin.x += rightViewInsets.right + rect.origin.y += rightViewInsets.top + return rect + } + + public override func leftViewRectForBounds(bounds: CGRect) -> CGRect { + var rect = super.leftViewRectForBounds(bounds) + rect.origin.x += leftViewInsets.left + rect.origin.y += leftViewInsets.top + return rect + } +}