最简单的方法是将右对齐的文本标签放在文本字段上,这样会留下对齐的文本。
当用户开始编辑的文本框,设置货币符号:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currencyLabel.text = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
}
如果你想保持它作为文本框的文本部分,就显得有点复杂,因为你需要让他们从删除符号,一旦你把它放在那里:
// Set the currency symbol if the text field is blank when we start to edit.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.text.length == 0)
{
textField.text = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Make sure that the currency symbol is always at the beginning of the string:
if (![newText hasPrefix:[[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]])
{
return NO;
}
// Default:
return YES;
}
由于@Aadhira指出的那样,你也应该用一个数字格式化,因为你是它向用户显示格式化的货币。
你说的“Xcode的货币符号”是什么意思? – 2012-11-25 14:06:31