相关疑难解决方法(0)

在UITextfield中使用NSNumberFormatter进行实时格式化

我有UITextfield,用户可以输入一个美元金额,我希望textfield总是用两位小数格式化($.##).必须始终保持格式化.但我仍然坚持如何将输入的数字附加到文本字段中的现有数字?

//This delegate is called everytime a character is inserted in an UITextfield.
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([textField tag] == amountTag)
    {

        NSString *amount = string;

//How do I append the already entered numbers in the textfield to the new entered values   ?
//??

        //Get new formatted value
        NSString *newAmount = [self formatCurrencyValue:[amount doubleValue]];

        [textField setText:[NSString stringWithFormat:@"%@",newAmount]];

        return NO;
    }

    //Returning yes allows the entered chars to be processed
    return YES;
}


-(NSString*) formatCurrencyValue:(double)value
{ …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa-touch objective-c

6
推荐指数
1
解决办法
6312
查看次数

如何限制文本字段在swift中只接受十进制值

我想在我的文本字段中只接受十进制值。

下面的代码允许我只输入数字和 '.' 但我们可以输入多个 '.'

我如何才能将其限制为一个 '.' 或者如何在 swift 3.1 中限制文本字段只接受十进制值

let aSet = NSCharacterSet(charactersIn:"0123456789.").inverted
let compSepByCharInSet = r_Qty_txt.text?.components(separatedBy: aSet)
let numberFiltered = compSepByCharInSet?.joined(separator: "")
Run Code Online (Sandbox Code Playgroud)

我的目标设备是 iPad。

listViewCell.swift 代码

import UIKit

class listViewCell: UITableViewCell, UITextFieldDelegate {

var delegate: CellInfoDelegate?

@IBOutlet weak var desc_lbl: UILabel!
@IBOutlet weak var openQty_lbl: UILabel!
@IBOutlet weak var r_Qty_txt: UITextField!
@IBOutlet weak var itemId_lbl: UILabel!

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let newString: String = (textField.text! as NSString).replacingCharacters(in: range, …
Run Code Online (Sandbox Code Playgroud)

uitextfield ipad ios swift swift3

0
推荐指数
1
解决办法
2900
查看次数

标签 统计

cocoa-touch ×1

ios ×1

ipad ×1

iphone ×1

objective-c ×1

swift ×1

swift3 ×1

uitextfield ×1