本文介绍了在UITextField的.NumberPad键盘上添加减号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS开发的新手,请原谅我提出的一些显而易见的问题.众所周知,UITextField的KeyboardType设置为.NumberPad的键盘如下所示...

Fairly new to iOS development so forgive me for asking something that might be quite obvious. As you all know the UITextField's keyboard with keyboardType set to .NumberPad looks like the following...

.NumberPad键盘

我想做的是用减号替换左下角的空白区域.这可能吗?或者需要编写一个完整的自定义键盘来实现这一目标吗?

What I would like to do is replace the empty space in the lower left corner with a minus sign. Is this possible or does one need to write an entire custom keyboard to achieve this?

非常感谢您的帮助.

推荐答案

在您的文本字段inputAccessoryView中添加工具栏,当文本字段成为响应者时,键盘将显示工具栏(Swift 3.0):

Add a toolbar to your textfield inputAccessoryView and when the textfield will become the responder then the keyboard will show the toolbar (Swift 3.0):

func addToolBar(){
   let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
   let minusButton = UIBarButtonItem(title: "-", style: .plain, target: self, action: #selector(toggleMinus))
   toolbar.items = [minusButton]
   theTextField.inputAccessoryView = toolbar
}

func toggleMinus(){

    // Get text from text field
    if var text = theTextField.text , text.isEmpty == false{

        // Toggle
        if text.hasPrefix("-") {
            text = text.replacingOccurrences(of: "-", with: "")
        }
        else
        {
            text = "-\(text)"
        }

        // Set text in text field
        theTextField.text = text

    }
}

希望有帮助.

这篇关于在UITextField的.NumberPad键盘上添加减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:14