gpt4 book ai didi

ios - Swift 中文本字段的百分比掩码

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:53 33 4
gpt4 key购买 nike

我一直在尝试为以值 0.00% 开头的文本字段实现百分比掩码。如果用户点击“1”,它将变为 0.01%。如果他们然后点击 0,它变为 0.10%,依此类推。

我已经用货币实现了这一点。而且我已经非常接近使用百分比,但无法准确地创建我想要的东西。

因此,字段最初设置为 0.00%。我想显示百分比符号,我希望最大数字为 100.00%,我希望最大小数位数为 2,并且希望在用户键入时更新该字段,我希望他们能够按退格键删除最后输入的数字。哦,当它达到最大值 100.00 时,按另一个数字不会重置该值,也不会做任何奇怪的事情。

这是我试过的代码。我也尝试了很多变体:

func textFieldDidChanged(textField: UITextField) {
numberFormatter.numberStyle = .PercentStyle
numberFormatter.maximumFractionDigits = 2
numberFormatter.minimumFractionDigits = 2
numberFormatter.maximum = 100
let text = textField.text!.stringByReplacingOccurrencesOfString(numberFormatter.percentSymbol, withString: "").stringByReplacingOccurrencesOfString(numberFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(numberFormatter.decimalSeparator, withString: "")
textField.text = numberFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
}

我已经查看了所有内容,并且在这方面花费了太多时间。请帮忙。

最佳答案

这就是我想到的(对于我的应用程序,我想付出的努力差不多)。需要处理两种情况:1) 输入和 2) 删除。处理输入是比较容易的部分(从以前的 post 修改而来)。处理删除更难实现。基本上当检测到退格键时,向右移动一位小数。

处理输入:

mInputRate.addTarget(self, action: #selector(rateTextFieldChanged), for: .editingChanged)
...
@objc func rateTextFieldChanged (_ textField : UITextField){
if let amountString = textField.text?.rateInputFormatting() {
textField.text = amountString
calculate()
}
}

extension String {

// formatting text for currency textField
func rateInputFormatting() -> String {

var number: NSNumber!
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 3
formatter.minimumFractionDigits = 3
formatter.maximum = 100
formatter.minimum = 0

var amountWithPrefix = self

// remove from String: "$", ".", ","
let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count), withTemplate: "")

let double = (amountWithPrefix as NSString).doubleValue
number = NSNumber(value: (double / 1000))

// if first number is 0 or all numbers were deleted
guard number != 0 as NSNumber else {
return ""
}

return "\(formatter.string(from: number)!)%"
}
}

这处理删除(退格):

extension CalculatorVC : UITextFieldDelegate{
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let char = string.cString(using: String.Encoding.utf8)!

let isBackSpace = strcmp(char, "\\b")

if (isBackSpace == -92) {
//delete the second to last character
let text = textField.text!
let textDouble = text.replacingOccurrences(of: "%", with: "")
let doubleAdjusted = Double(textDouble)! / 10.0

//pad to three decimal places
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 3
formatter.minimumFractionDigits = 3
formatter.roundingMode = .down

let num = NSNumber(value: doubleAdjusted)
let newText = formatter.string(from: num)!
textField.text = "\(newText)%"

}
return true
}
}

关于ios - Swift 中文本字段的百分比掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35770130/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com