Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.
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
16 changes: 13 additions & 3 deletions Readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand All @@ -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.
66 changes: 0 additions & 66 deletions SAMTextField/SAMTextField.h

This file was deleted.

81 changes: 0 additions & 81 deletions SAMTextField/SAMTextField.m

This file was deleted.

103 changes: 103 additions & 0 deletions SAMTextField/SAMTextField.swift
Original file line number Diff line number Diff line change
@@ -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
}
}