【问题标题】:UITextField how to make the .text always displaying '$' sign while typing numbers on the right side?UITextField如何在右侧输入数字时使.text始终显示'$'符号?
【发布时间】:2016-03-17 02:46:51
【问题描述】:

我有一个 UITextField,我正在接受一些输入,这些输入在这种护理中是美元。我需要实现如何在输入数字后始终显示“$”符号(我的意思是光标右侧(而不是左侧)的“$”,因为我将为欧元编写代码(€)标志)。有什么想法吗?

【问题讨论】:

    标签: ios swift uitextfield ios9


    【解决方案1】:

    您可以使用 UITextField 委托或收听通知,例如 UITextFieldTextDidChangeNotification
    在这些方法中,检查文本并添加“$”。

    由于 '$' 应该在正确的位置,我建议使用 UITextField 属性selectedTextRange 来计算位置,然后使用setSelectedTextRange 将光标移动到正确的位置。

    UITextPosition * tp = [self.passwordTextField positionFromPosition:self.passwordTextField.beginningOfDocument  offset:self.passwordTextField.text.length - @"$".length];
    UITextRange *sr = [self.passwordTextField textRangeFromPosition:tp toPosition:tp];
    

    我认为sr 是 selectedTextRange 的值。

    【讨论】:

    • @childrenOurForture 我在配置 UIAlertController 的闭包 addTextFieldWithConfigurationHandler 中。我在里面有一个通知中心,正在监听 UITextFieldTextDidChangeNotification。但是当我实时输入值时,我很难实现'$'或'€'符号总是在光标的右边:(你能给我更多的提示吗兄弟?我需要吗?以某种方式将光标替换到“€”的左侧,例如对于每个 newValue?
    • 还有一件事兄弟,相当于setSelectedTextRange的方法是什么?我在 Swift 2.1.1 中,但我找不到它:(
    • @BradSmith 在swift中是selectedTextRange,直接用xxx.selectedTextRange = xxx,行吗?
    • 兄弟,我坚持设置 selectedTextRange 属性。我创建了一个子字符串,获取文本的结束值并将 € 符号删除 2(考虑到“空格”)。但是这个属性接受 UITextPosition 并且我只有子字符串的索引。我坚持这一点。但是,为了证明我在正确的轨道上,我尝试使用 selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.beginningOfDocument) 并且它正在工作,除了它总是将光标移动到前面。现在我需要设法创建这个范围:(
    • 好兄弟,这就是我想要的。它干净、紧凑、简洁,没有脏的长代码:D
    【解决方案2】:

    实现这个 UITextField 委托方法,我认为这对你有用。

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
    let currentCharacterCount = textField.text?.characters.count ?? 0
    
    if (range.length + range.location > currentCharacterCount){
      return false
    }
    
    var cleanString: String = ""
    
    if let text = textField.text{
    
      let cleanStringAry = text.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet) as NSArray
    
      cleanString = cleanStringAry.componentsJoinedByString("")
    
    }
    
    var cellAmount = 0
    
    if cleanString.characters.count > 0{
      cellAmount = Int(cleanString)!
    }
    
    if let fff:String = string{
    
      if fff.characters.count > 0{
    
        cellAmount = cellAmount * 10 + Int(fff)!
    
      }else{
        cellAmount = cellAmount / 10
      }
    
    
    }
    
    
    let amount = NSNumber(float: Float(cellAmount)/100.0)
    
    let currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = .CurrencyStyle
    currencyFormatter.currencyCode = "\u{20AC}"
    currencyFormatter.positiveSuffix = "\u{20AC}"
    currencyFormatter.paddingPosition = .AfterSuffix
    currencyFormatter.negativeSuffix = "\u{20AC}"
    currencyFormatter.negativeFormat = "-¤#,##0.00"
    
    let result = currencyFormatter.stringFromNumber(amount)
    if result == "0.00\u{20AC}"{
      textField.text = ""
    }else{
      textField.text = currencyFormatter.stringFromNumber(amount)
    
    }
    
    return false
    }
    

    【讨论】:

    • 我认为我的项目兄弟不需要所有这些代码。我需要做的是设置 selectRangeProperty,它将光标向左移动到 € 前面减去第 n 个子字符串索引。我在设置接受 UITextPosition 的 selectRangeProperty 时遇到了一些问题
    【解决方案3】:

    我建议添加一个标签并将其文本设置为'$'。观察textField的文本,当用户输入时,更新标签框架。像这样:

    let label = UILabel(frame: CGRect(x: 10, y: 10, width: 30, height: 20))
    label.text = "$"
    label.font = contentTextField.font
    label.textColor = textField.textColor
    textField.superview?.addSubview(label)
    
    textField.addTarget(self, action: "yourFunc", forControlEvents: .EditingChanged)
    
    func yourFunc(sender:UITextField) {
        // calculate the width of sender.text
        // update the frame of label
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2011-06-23
      相关资源
      最近更新 更多