ios - 高级/语义 UITextField 设置

标签 ios uitextfield

我觉得配置 UITextField 的工作比它应该做的要多。我目前正在开发的应用程序需要少量文本字段:

  • 电子邮件输入 – 从不纠正并使用电子邮件键盘。
  • 密码输入 – 从不纠正,使用标准键盘,模糊输入。
  • 标题文本 – 更正,使用标准键盘,首字母大写。
  • 其他文本 – 更正,使用标准键盘,将句子的第一个字母大写。

… 大部分就是这些。

所以,而不是:

[self.txfPassword setSecureTextEntry: YES];
[self.txfPassword setAutocorrectionType: UITextAutocorrectionTypeNo];
[self.txfPassword setAutocapitalizationType: UITextAutocapitalizationTypeNone];
[self.txfPassword setSpellCheckingType: UITextSpellCheckingTypeNo];
[self.txfPassword setKeyboardType: UIKeyboardTypeDefault];

我想做的事:

[self.txfPassword setPurpose: UITextFieldPurposePassword];

是否有某种内置的东西可以做到这一点,还是我要自己动手?

理想情况下,这应该是内置的,以便:

  • 当 iOS 发现我正在使用电子邮件地址时,它会提供插入地址簿中的电子邮件,并且也可以执行正确的验证。
  • 或者 Twitter 句柄/FB 身份将用于搜索我的 friend ,然后搜索其他公开身份。
  • 或者,如果我使用密码,它会生成一个随机字符串并将其插入钥匙串(keychain)。
  • 或者如果是电话号码,它可以在地址簿等中查找。

谢谢。

最佳答案

您可以轻松地为这类事物创建一个类别...

// .h
typedef NS_ENUM(NSUInteger, TextFieldPurpose) {
    TextFieldPurposePassword,
    TextFieldPurposeEmail,
    TextFieldPurposeBlah,
};

@interface UITextField (Purpose)

- (void)setPurpose:(TextFieldPurpose)purpose;

@end

// .m
@implementation UITextField (Purpose)

- (void)setPurpose:(TextFieldPurpose)purpose
{
    switch(purpose) {
        case TextFieldPurposePassword:
            [self setPasswordPurpose];
            break;
        case TextFieldPurposeEmail:
            [self setEmailPurpose];
            break;
        case TextFieldPurposeBlah:
            // set some other purpose
            break;
    }
}

- (void)setEmailPurpose
{
    // do your email set up here...
}

- (void)setPasswordPurpose
{
    [self setSecureTextEntry: YES];
    [self setAutocorrectionType: UITextAutocorrectionTypeNo];
    [self setAutocapitalizationType: UITextAutocapitalizationTypeNone];
    [self setSpellCheckingType: UITextSpellCheckingTypeNo];
    [self setKeyboardType: UIKeyboardTypeDefault];
}

@end

现在,您不必在文本字段上进行所有配置,只需 #import UITextField+Purpose.h" 然后...

[self.txfPassword setPurpose:TextFieldPurposePassword];

关于ios - 高级/语义 UITextField 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356552/

相关文章:

ios - Xcode、HMSoft HM-10 BLE不选择UUID直接连接一个模块

ios - 在 iOS 上播放 youtube 视频

ios - 检查 UITextFields 集合中的文本字段是否为空

ios - 当键盘出现时 UITableViewController 没有向上滚动事件文本字段的任何原因?

ios - UITextField 目标/ Action 与委托(delegate)方法

ios - 尝试使用 Swift 将文本字段中每个单词的首字母大写

ios - 如何摆脱警告 "Result of call ' resignFirstResponder( )' is unused"?

ios - 如何在推送通知中正确使用 UDID?

swift - 如何在 Swift 中限制 UITextField 中的某些字符?

ios - 使用 Codable 解码具有多个 key 的 JSON