在我的iPhone应用程序中,有一个文本字段用于输入用户的电话号码。我已尝试过数字键盘,手机键盘,小数点等。但没有一个是合适的。
如何设置包含“ - ”键的数字键盘?
答案 0 :(得分:2)
您可以添加 - 您想要添加的内容,如下面的代码会在文本字段中输入数字时添加brackets (
和dash -
。
#pragma mark - Phone Number Field Formatting
// Adopted from: http://stackoverflow.com/questions/6052966/phone-number-formatting
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.mobileNumberField) {
int length = [self getLength:textField.text];
if(length == 10) {
if(range.length == 0)
return NO;
}
if(length == 3) {
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"(%@) ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
else if(length == 6) {
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
}
}
return YES;
}
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
if(length > 10) {
mobileNumber = [mobileNumber substringFromIndex: length-10];
}
return mobileNumber;
}
-(int)getLength:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
return length;
}
答案 1 :(得分:2)
我认为您可以使用UIKeyboardTypePhonePad
并使用inputAccessoryView
UITextField
属性作为-
按钮。
UIToolbar *punctuationbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"-" style:UIBarButtonItemStyleBordered target:self action:@selector(addHyphen)],
nil];
[numberToolbar sizeToFit];
_yourTextField.inputAccessoryView = punctuationbar;
并添加addHyphen
方法,如:
- (void)addHyphen
{
_yourTextField.text = [_yourTextField.text stringByAppendingString:@"-"];
}
注意:此方法会在文本末尾附加-
。您需要修改上述方法。
<强> inputAccessoryView 强>
文本字段成为时显示的自定义附件视图 第一响应者
@property (readwrite, retain) UIView *inputAccessoryView;
<强>讨论强>
此属性的默认值为nil。为此分配视图 属性使该视图显示在标准系统上方 键盘(或自定义输入视图上方,如果提供一个)时 文本字段成为第一个响应者。例如,你可以使用 此属性用于将自定义工具栏附加到键盘。可用性
Available in iOS 3.2 and later.
在UITextField.h中声明