ios - 如何在 Swift 中自定义安全文本字段点

标签 ios swift iphone uitextfield

我想更改安全文本字段点。

默认情况下,我们得到的安全字段是点。但我需要将它们更改为

Like this

我需要将点更改为这种方式,请提出任何建议。

最佳答案

下面是一个将您的 passwordTextField 点转换为“*”的示例代码,您可以修改此代码以使用您拥有的任何自定义字体中的自定义字符来给出红心:

@IBOutlet weak var passwordTextField: UITextField!
var passwordText = ""

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == passwordTextField {

        var hashPassword = String()
        let newChar = string.first
        let offsetToUpdate = passwordText.index(passwordText.startIndex, offsetBy: range.location)

        if string == "" {
            passwordText.remove(at: offsetToUpdate)
            return true
        }
        else { passwordText.insert(newChar!, at: offsetToUpdate) }

        for _ in 0..<passwordText.count {  hashPassword += "*" }
        textField.text = hashPassword
        return false
    }
    return true
}

关于ios - 如何在 Swift 中自定义安全文本字段点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62047404/

相关文章:

ios - facebook ios sdk 2.0 于 2012 年 9 月 7 日停止工作?

swift - 在 SwiftUI 中延迟转换

ios - 如何在谷歌地图iOS Sdk中拖动圆形 View

ios - 在运行 iOS 11 的 iPad 上禁用 anchor 预览

iphone - 如何将 NSdata 从一部 iPhone 传输到另一部

ios - 如何通过 Segue 在 vi​​ewWillAppear 中划分案例?

ios - 在后台检查服务器端应用程序通知,在 Swift 中

iphone - 如何使用 UIAppearance 为子类而不是父类(super class)设置样式?

iphone - 使用 MKCoordinateRegion 将 MKMapView 聚焦在 NSString 经纬度坐标上

ios - watchOS 2 WatchConnectivity Apple Watch 和 iPhone 之间的时间延迟,同时将数据从一个发送到另一个?