iOS替换uitextfield中的char [英] iOS Replace char in uitextfield
本文介绍了iOS替换uitextfield中的char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现了一个恼人的问题。我有一个带有数字十进制键盘的文本字段,但模拟器有点,设备有逗号(均为6.1)。我该怎么办,如果你插入一个逗号,它会被一个点替换?
I found an annoying problem. I have a textfield with the numeric decimal keyboard, but the simulator has the point, the device has the comma (both 6.1). How can I do, if you insert a comma, this is replaced by a point?
谢谢
编辑:
此过程必须在此方法中实时进行
this process, must take place in this method, in real time
- (BOOL) textField: (UITextField *) theTextField shouldChangeCharactersInRange: (NSRange) range ReplacementString: (NSString *) string
否则写入逗号,并在下一个字符处替换它
otherwise the comma is written, and at the next character, it is replaced
推荐答案
您可以使用
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
例如:
NSString *str = myTextfield.text;
str = [str stringByReplacingOccurrencesOfString:@"."
withString:@","];
自定义键盘的
请参阅 link
编辑
您可以检查此函数中的字符串输入以及是否为@。然后你可以用@,
Edit You can check the string enter in this function and if its @"." then you can change it with @","
// using something this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self updateTextLabelsWithText: newString];
NSLog(@"Changed Str: %@",newString);
return YES;
}
-(void)updateTextLabelsWithText:(NSString *)str
{
str = [str stringByReplacingOccurrencesOfString:@"."
withString:@","];
[myTextFiled setText:str];
}
这篇关于iOS替换uitextfield中的char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文