0

我有一个文本字段,我想用数字键盘在其中输入。我希望文本字段一开始就像 00.00,当用户输入它时,它会从左到右或从右到左更新。例如,如果他键入 2 后跟 3,则文本字段应该是 23.00,并且用户只允许输入小数点后最多 2 位的数字。我怎样才能获得这个功能?

提前致谢。

干杯, Siavash

4

3 回答 3

2

.h

@property (assign, nonatomic) int maximumFractionDigits;
@property (strong, nonatomic) NSString* decimalSeparator;

loadView放入或viewDidLoad放入此,

NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencyCode:@"GBP"];
self.maximumFractionDigits = numberFormatter.maximumFractionDigits;
self.decimalSeparator = numberFormatter.decimalSeparator;
[numberFormatter release];

UITextFieldDelegate方法

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
// get current cursor position
UITextRange* selectedRange = [textField selectedTextRange];
UITextPosition* start = textField.beginningOfDocument;
NSInteger cursorOffset = [textField offsetFromPosition:start toPosition:selectedRange.start];
// Update the string in the text input
NSMutableString* currentString = [NSMutableString stringWithString:textField.text];
NSUInteger currentLength = currentString.length;
[currentString replaceCharactersInRange:range withString:string];
// Strip out the decimal separator
[currentString replaceOccurrencesOfString:self.decimalSeparator withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [currentString length])];
// Generate a new string for the text input
int currentValue = [currentString intValue];
NSString* format = [NSString stringWithFormat:@"%%.%df", self.maximumFractionDigits];
double minorUnitsPerMajor = pow(10, self.maximumFractionDigits);
NSString* newString = [NSString stringWithFormat:format, currentValue / minorUnitsPerMajor];
textField.text = newString;
// if the cursor was not at the end of the string being entered, restore cursor position
if (cursorOffset != currentLength) {
    int lengthDelta = newString.length - currentLength;
    int newCursorOffset = MAX(0, MIN(newString.length, cursorOffset + lengthDelta));
    UITextPosition* newPosition = [textField positionFromPosition:textField.beginningOfDocument offset:newCursorOffset];
    UITextRange* newRange = [textField textRangeFromPosition:newPosition toPosition:newPosition];
    [textField setSelectedTextRange:newRange];
}

return NO;

}

于 2013-04-04T04:31:05.057 回答
1

您可以使用以下委托方法。根据您的要求进行更改。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *replacedSting = [textField.text stringByReplacingCharactersInRange:range withString:string];

NSArray *noOfDigits = [replacedSting componentsSeparatedByString:@"."];
if([noOfDigits count]>=2)
{
    NSString *sepStr=[NSString stringWithFormat:@"%@",[noOfDigits objectAtIndex:1]];
    return !([sepStr length]>1);
}
return YES;
}
于 2013-04-04T04:22:43.200 回答
0

如果您想在个位数之前设置十位数,这有点小技巧,但我认为它可以工作。您可以使用类似 UITextField 的委托方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

确保设置文本字段的委托,然后使用此方法。您可以自己保留一个计数器,以确定每次击键时要更改的索引。您会忽略该范围,并自己将其更改为您正在跟踪的范围。例如,从索引 0 开始并在每次调用该方法时递增,但在 X 秒内没有按下按钮后,重置为第一个索引。

否则,Ramu 的答案应该对你有用!

于 2013-04-04T04:25:21.497 回答