最簡單的方法是將右對齊的文本標籤放在文本字段上,這樣會留下對齊的文本。
當用戶開始編輯的文本框,設置貨幣符號:
- (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