在UITextField的开头添加恒定的国家/地区代码

尤西·萨法尔(Yossi Tsafar)

我有一个UITextField,用户需要在其中输入电话号码。

现在是这样的:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Auto-add hyphen before appending 4rd or 7th digit
    //
    //
    if (range.length == 0 && (range.location == 3 || range.location == 7))
    {
        textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string];
        return NO;
    }

    // Delete hyphen when deleting its trailing digit
    //
    //
    if (range.length == 1 && (range.location == 4 || range.location == 8))
    {
        range.location--;
        range.length = 2;
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""];
        return NO;
    }

    //  Prevent crashing undo bug – see note below.
    //
    //
    if (range.length + range.location > textField.text.length)
    {
        return NO;
    }

    //  Limit text field characters
    //
    //
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 12) ? NO : YES;
}

在第三个数字之后,我添加了一个连字符,然后再添加一个。我在这里想要实现的是在国家/地区的开头添加一个国家/地区代码作为常量,UITextField并且用户将无法删除该国家/地区代码假设是美国国家/地区代码,则UITextField文本在开始时看起来像这样,+1-然后在写完完整的数字后看起来像这样:+1-600-242-252

我怎样才能做到这一点?

提前致谢!

林德西·斯科特(Lyndsey Scott)

此答案假定一个起始国家代码字符串,该字符串的末尾包括连字符,例如:self.countryCode = @"+1-";文本字段最初应包含“ + 1-”。

我已经使回答的方式比您的原始意图更全面,因为它可以处理您忽略的许多用例,例如,具有多个字符的复制和粘贴操作,不适当的连字符删除,不适当的连字符添加,中线插入等。仍然不是很完美,因为您的原始答案在某些方面是不确定的...例如,如果您指定用户只能输入数字,则代码可以更简洁。

全文包含的注释中逐行描述了以下实现。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Combine the new text with the old
    NSMutableString *combinedText = [[textField.text stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%@", string]] mutableCopy];

    // If the user deletes part of the country code or tries
    // to edit it in any way, don't allow it
    if (combinedText.length < self.countryCode.length ||
        ![combinedText hasPrefix:self.countryCode]) {
        return NO;
    }

    //  Limit text field characters to 12
    if (combinedText.length > self.countryCode.length + 12) {
        return NO;
    }

    // If the user tries to add a hyphen where there's supposed
    // to be a hyphen, allow them to do so.
    if ([string isEqualToString:@"-"] &&
        (range.location == self.countryCode.length + 3 ||
        range.location == self.countryCode.length + 7)) {
        return  YES;
    }

    // Remove all the hyphens other than the one directly
    // following the country code        
    [combinedText replaceOccurrencesOfString:@"-" withString:@"" options:0 range:NSMakeRange(self.countryCode.length, [combinedText length] - self.countryCode.length)];

    // Auto-add the hyphens before the 4th and 7th digits
    if (combinedText.length > self.countryCode.length + 3)
        [combinedText insertString:@"-" atIndex:self.countryCode.length + 3];
    if (combinedText.length > self.countryCode.length + 7)
        [combinedText insertString:@"-" atIndex:self.countryCode.length + 7];

    // Store the original cursor position
    UITextPosition *pos = [textField selectedTextRange].start;

    // Count up the original number of hyphens
    NSUInteger originalNumberOfHyphens = [[textField.text componentsSeparatedByString:@"-"] count] - 1;
    // Count up the new number of hyphens
    NSUInteger newNumberOfHyphens = [[combinedText componentsSeparatedByString:@"-"] count] - 1;

    // Create a cursor offset to reflect the difference
    // in the number of hyphens
    float offset = newNumberOfHyphens - originalNumberOfHyphens;

    // Update the text field to contain the combined text
    textField.text = combinedText;

    // Update the cursor position appropriately
    if (string.length > 0) {
        UITextPosition* cursor = [textField positionFromPosition:[textField beginningOfDocument] offset:range.location + range.length + offset + string.length];
        textField.selectedTextRange = [textField textRangeFromPosition:cursor toPosition:cursor];
    } else {
        UITextPosition* cursor = [textField positionFromPosition:pos inDirection:UITextLayoutDirectionLeft offset:1-offset];
        textField.selectedTextRange = [textField textRangeFromPosition:cursor toPosition:cursor];
    }

    // No need to replace the string since it's already been done
    return NO;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

国家/地区代码验证

来自分类Dev

按国家/地区代码搜索国家

来自分类Dev

从python中的国家/地区代码中获取国家/地区名称?

来自分类Dev

Magento:按国家/地区名称获取国家/地区代码

来自分类Dev

从python中的国家/地区代码中获取国家/地区名称?

来自分类Dev

具有相同国家代码的国家/地区的国家/地区代码选择下拉问题

来自分类Dev

ASP.Net MVC:如何将国家/地区代码添加到所有url

来自分类Dev

如何在Flutter中将国家/地区拨号代码添加到TextEditingController

来自分类Dev

Ionic Framework如何使用国家/地区代码为手机号码添加表格?

来自分类Dev

数据图获取国家/地区代码列表

来自分类Dev

德尔福:通过LocaleID获取国家/地区代码

来自分类Dev

如何从CultureInfo获取国家/地区代码?

来自分类Dev

从VBA和LDAP获取国家/地区代码

来自分类Dev

XQuery函数或ISO国家/地区代码库?

来自分类Dev

Zend:从国家/地区代码获取语言环境

来自分类Dev

获取QLocale :: Country的国家/地区代码

来自分类Dev

NSLocale国家/地区代码,名称为Null

来自分类Dev

将三个字母的国家/地区代码转换为完整的国家/地区名称

来自分类Dev

使用Python DictReader / DictWriter将国家/地区名称转换为国家/地区代码

来自分类Dev

如何使用R将国家/地区代码转换为数据框内列中的国家/地区名称?

来自分类Dev

Laravel使用访问器从国家/地区代码返回国家/地区名称

来自分类Dev

使用非英语语言将国家/地区代码转换为设备上的国家/地区名称

来自分类Dev

如何从C#中的特定国家/地区获取国家/地区代码?

来自分类Dev

在 WooCommerce 中显示完整的国家/地区名称而不是国家/地区代码?

来自分类Dev

如何根据 Xamarin.Forms 中的国家/地区代码获取国家/地区图像?

来自分类Dev

.htaccess RewriteRule国家/地区代码作为获取参数

来自分类Dev

Swift 2国家/地区代码的电话号码

来自分类Dev

使用NodaTime按纬度和经度获取国家/地区代码

来自分类Dev

根据国家/地区代码规范化电话号码

Related 相关文章

  1. 1

    国家/地区代码验证

  2. 2

    按国家/地区代码搜索国家

  3. 3

    从python中的国家/地区代码中获取国家/地区名称?

  4. 4

    Magento:按国家/地区名称获取国家/地区代码

  5. 5

    从python中的国家/地区代码中获取国家/地区名称?

  6. 6

    具有相同国家代码的国家/地区的国家/地区代码选择下拉问题

  7. 7

    ASP.Net MVC:如何将国家/地区代码添加到所有url

  8. 8

    如何在Flutter中将国家/地区拨号代码添加到TextEditingController

  9. 9

    Ionic Framework如何使用国家/地区代码为手机号码添加表格?

  10. 10

    数据图获取国家/地区代码列表

  11. 11

    德尔福:通过LocaleID获取国家/地区代码

  12. 12

    如何从CultureInfo获取国家/地区代码?

  13. 13

    从VBA和LDAP获取国家/地区代码

  14. 14

    XQuery函数或ISO国家/地区代码库?

  15. 15

    Zend:从国家/地区代码获取语言环境

  16. 16

    获取QLocale :: Country的国家/地区代码

  17. 17

    NSLocale国家/地区代码,名称为Null

  18. 18

    将三个字母的国家/地区代码转换为完整的国家/地区名称

  19. 19

    使用Python DictReader / DictWriter将国家/地区名称转换为国家/地区代码

  20. 20

    如何使用R将国家/地区代码转换为数据框内列中的国家/地区名称?

  21. 21

    Laravel使用访问器从国家/地区代码返回国家/地区名称

  22. 22

    使用非英语语言将国家/地区代码转换为设备上的国家/地区名称

  23. 23

    如何从C#中的特定国家/地区获取国家/地区代码?

  24. 24

    在 WooCommerce 中显示完整的国家/地区名称而不是国家/地区代码?

  25. 25

    如何根据 Xamarin.Forms 中的国家/地区代码获取国家/地区图像?

  26. 26

    .htaccess RewriteRule国家/地区代码作为获取参数

  27. 27

    Swift 2国家/地区代码的电话号码

  28. 28

    使用NodaTime按纬度和经度获取国家/地区代码

  29. 29

    根据国家/地区代码规范化电话号码

热门标签

归档