我在UITextField中使用了一种自定义字体,它已经打开了secureTextEntry.当我在单元格中键入时,我会看到我 Select 的字体中的项目符号,但当字段失go 焦点时,这些项目符号会恢复为系统标准字体.如果我再次点击该字段,它们会变回我的字体,依此类推.

有没有什么方法可以确保它们继续显示自定义字体的项目符号,即使字段不对焦?

enter image description here enter image description here

推荐答案

解决这个问题的子类.创建arbitrary 100,然后将secure属性设置为YES(通过IB中的KVC).

实际上,它实现了lukech建议的注释.当textfield结束编辑时,它会切换到一个任意的textfield,然后在其中设置大量的点,并在text访问器中进行一些黑客攻击,以始终获得该字段所包含的实际文本.

@interface SecureTextFieldWithCustomFont : UITextField
@property (nonatomic) BOOL secure;
@property (nonatomic, strong) NSString *actualText;
@end


@implementation SecureTextFieldWithCustomFont


-(void)awakeFromNib
{
    [super awakeFromNib];

    if (self.secureTextEntry)
    {
        // Listen for changes.
        [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
        [self addTarget:self action:@selector(editingDidChange) forControlEvents:UIControlEventEditingChanged];
        [self addTarget:self action:@selector(editingDidFinish) forControlEvents:UIControlEventEditingDidEnd];
    }
}

-(NSString*)text
{
    if (self.editing || self.secure == NO)
    { return [super text]; }

    else
    { return self.actualText; }
}

-(void)editingDidBegin
{
    self.secureTextEntry = YES;
    self.text = self.actualText;
}

-(void)editingDidChange
{ self.actualText = self.text; }

-(void)editingDidFinish
{
    self.secureTextEntry = NO;
    self.actualText = self.text;
    self.text = [self dotPlaceholder];
}

-(NSString*)dotPlaceholder
{
    int index = 0;
    NSMutableString *dots = @"".mutableCopy;
    while (index < self.text.length)
    { [dots appendString:@"•"]; index++; }
    return dots;
}


@end

可能会被扩展以处理非NIB实例化、处理默认值等,但您可能已经明白了这一点.

Objective-c相关问答推荐

单例实例与类方法

应用程序终止时处理推送通知

iPhone SDK 网络连接检测

TWTweetComposeViewController 在 IOS6 中已弃用

NSInternalInconsistencyException', reason: '试图将第 0 行插入第 0 节,但更新后第 0 节只有 0 行'

Xcode 是否支持区域?

NSPredicate 与整数比较

UITableView - Select 了哪一行?

设置数据后调整 UICollectionView 单元格的大小

如何删除应用程序可访问的所有 keys 串项目?

检测 iOS 应用程序是否在调试器中运行

使用 CocoaPods 时如何向 Xcode 添加自定义项目配置?

UICollectionView 自动调整高度

如何在 Cocoa 中获取当前日期

ARC 下的 NSString 属性应该是强的还是复制的?

iPhone - dequeueReusableCellWithIdentifier 用法

将自定义子视图(在 xib 中创建)添加到视图控制器的视图 - 我在做什么错

哪个是正确的,nil 或 NULL,来标记没有 Objective-C 块?

Objective-C:如何向 NSURL 添加查询参数?

如何在对象内使用 objc_setAssociatedObject/objc_getAssociatedObject?