BxTextField 1.12.4

BxTextField 1.12.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jun 2023
SPMSupports SPM

Maintained by Sergey Balalaev.



  • By
  • Sergey Balalaev

BxTextField

This component available put formatted text, for example: phone number, url or email address, credit card with comfortable format. Also you can use other features what we have in UITextField whitout issues from Apple.

Gif demo

Demo Gif

Features

  • Inherited from UITextField and don't use delegate
  • Can use patterns sides with constant text
  • Have formatting putting with different filling direction
  • Correct working with selecting/editing text
  • Fixed native issues (problems with edges)
  • Covered of Tests

Requirements

  • iOS 8.0+ : iOS 8.x/9.x/10.x/11.x/12.x/13.x/14.x
  • Swift 3.0+ : Swift 3.2/4.x/5.x supported

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate BxTextField into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
pod 'BxTextField', '~> 1.4'
end

Then, run the following command:

$ pod install

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but BxTextField does support its use on supported platforms.

Once you have your Swift package set up, adding BxTextField as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .Package(url: "https://github.com/ByteriX/BxTextField.git", majorVersion: 1)
]

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate BxTextField into your project manually.

Embedded Framework

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add BxTextField as a git submodule by running the following command:
$ git submodule add https://github.com/ByteriX/BxTextField.git
  • Add all sources and resources from local copy of BxTextField to the build phase of the project.

  • And that's it!

Properties

Formatting

  • formattingTemplate: String, Format text for putting pattern. If formattingReplacementChar is "" then example may has value "*** - **** - ********". Default is ""

  • formattingReplacementChar: String, Replacement symbol, it use for formattingTemplate as is as pattern for replacing. Default is "#"

  • formattingEnteredCharacters: String, Allowable symbols for entering. Uses only if formattingTemplate is not empty. Default is "", that is all symbols.

  • formattingDirection: FormattingDirection, Direction for replacement text from template. Can equal leftToRight or rightToLeft value. Default is leftToRight

  • isFormattingRewriting: Bool, When user would try put extra symboles for filled text and if it's true then text will be rewrited. It depends from formattingDirection too.

Patterns

  • rightPatternText: String, Not editable pattern part of the text. Defaults to "".
  • leftPatternText: String, Not editable pattern part of the text. Defaults to "".

Usage

Example

class SimpleController: UIViewController {
	
	@IBOutlet var urlField: BxTextField!
	@IBOutlet var phoneField: BxTextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        urlField.leftPatternText = "http://"
        urlField.rightPatternText = ".com"
        urlField.addTarget(self, action: #selector(urlDidChange(sender:)), for: .editingChanged)
        
        phoneField.leftPatternText = "+4 "
        phoneField.formattingReplacementChar = "*"
        phoneField.formattingTemplate = "(***) - ** - ** - **"
        phoneField.formattingEnteredCharacters = "0123456789"
        phoneField.addTarget(self, action: #selector(phoneDidChange(sender:)), for: .editingChanged)
    }
    
    @IBAction func urlDidChange (sender: BxTextField) {
        print(sender.enteredText) // it should show "your.inputed.domain.only"
    }
    
    @IBAction func phoneDidChange (sender: BxTextField) {
        print(sender.text) // it should show "+4 (123) - 45 - 67 - 89"
    }
    
}

Different filling direction

class BxTextFieldFormattingTests: XCTestCase {

    func testRightToLeftDirection() {
        let textField = BxTextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        textField.formattingTemplate = "#.##"
        textField.formattingDirection = .rightToLeft
        textField.enteredText = "1"
        XCTAssertEqual(textField.text!, "1")
        textField.enteredText = "12"
        XCTAssertEqual(textField.text!, ".12")
        textField.enteredText = "123"
        XCTAssertEqual(textField.text!, "1.23")
    }
    
    func testLeftToRightDirection() {
        let textField = BxTextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        textField.formattingTemplate = "#.##"
        textField.formattingDirection = .leftToRight
        textField.enteredText = "1"
        XCTAssertEqual(textField.text!, "1")
        textField.enteredText = "12"
        XCTAssertEqual(textField.text!, "1.2")
        textField.enteredText = "123"
        XCTAssertEqual(textField.text!, "1.23")
    }
}

License

BxTextField is released under the MIT license. See LICENSE for details.