BxTextField 版本 1.12.4

BxTextField 版本 1.12.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2023年6月
SPM支持 SPM

Sergey Balalaev 维护。



BxTextField 版本 1.12.4

  • Sergey Balalaev

BxTextField

此组件可将格式化文本(例如电话号码、URL或电子邮件地址、信用卡)放置得非常舒适。您还可以使用我们提供的其他功能,而不必担心与 Apple 相关的问题。

Gif 演示

Demo Gif

功能

  • 继承自 UITextField 而不使用代理
  • 可以使用constant text两侧的图案
  • 支持不同填充方向的格式化放置
  • 正确处理文本的选中/编辑
  • 修复了原生问题(边缘问题)
  • 提供测试覆盖

需求

  • 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

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装:

$ gem install cocoapods

要将 BxTextField 集成到您的 Xcode 项目中使用 CocoaPods,请在您的 Podfile 中指定它

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

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

然后,运行以下命令

$ pod install

Swift 包管理器

Swift 包管理器 是一个自动化 Swift 代码分发的工具,它集成到 swift 编译器中。它仍在早期开发阶段,但 BxTextField 支持在支持平台上的使用。

一旦您的 Swift 包设置完毕,将 BxTextField 添加为依赖项就像将其添加到您的 Package.swift 中的 dependencies 值一样简单。

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

手动操作

如果您不想使用上述任何依赖性管理器,您可以手动将 BxTextField 集成到项目中。

嵌入式框架

  • 打开终端,cd 到您的顶级项目目录,如果您项目尚未初始化为git仓库,运行以下命令
$ git init
  • 通过以下命令将 BxTextField 添加为git 子模块
$ git submodule add https://github.com/ByteriX/BxTextField.git
  • BxTextField 本地副本中的所有源和资源添加到项目的构建阶段。

  • 就这么简单!

属性

格式化

  • formattingTemplate: 字符串,用于放置模式的文本格式。如果 formattingReplacementChar 是 "" 则示例可能为 "*** - **** - ********"。默认为 ""

  • formattingReplacementChar: 字符串,替换符号。用作 formattingTemplate 中的模式进行替换。默认为 "#"

  • formattingEnteredCharacters: 字符串,允许输入的符号。如果在 formattingTemplate 不为空的情况下使用。默认为 "",即所有符号。

  • formattingDirection: FormattingDirection,从模板替换文本的方向。可以是 leftToRight 或 rightToLeft。默认为 leftToRight

  • isFormattingRewriting:.Bool,当用户试图为填充文本添加额外符号时,如果该值为true,则文本将被重写。这还取决于格式化方向。

模式

  • rightPatternText: String,文本不可编辑的模式部分。默认为""。
  • leftPatternText: String,文本不可编辑的模式部分。默认为""。

用法

示例

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"
    }
    
}

不同的填充方向

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")
    }
}

许可证

BxTextField遵循MIT许可证发布。详细信息请参阅LICENSE文件。