带有自定义字体的 UITextField secureTextEntry 项目符号? [英] UITextField secureTextEntry bullets with a custom font?

查看:18
本文介绍了带有自定义字体的 UITextField secureTextEntry 项目符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m using a custom font in a UITextField, which has secureTextEntry turned on. When I’m typing in the cell, I see the bullets in my chosen font, but when the field loses focus, those bullets revert to the system standard font. If I tap the field again, they change back to my font, and so on.

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

Is there a way I can ensure that they continue to display the custom font’s bullets, even when the field is out of focus?

推荐答案

解决此问题的子类.创建一个任意UITextField,然后将secure属性设置为YES(通过IB中的KVC).

A subclass that works this issue around. Create an arbitrary UITextField, then set the secure property to YES (via KVC in IB).

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

Actually it implements a comment suggested by lukech. When textfield ends editing, it switches to an arbitrary textfield, then set a bulk of dots into, and some hack in text accessor to always get the actual text the field holds.

@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 实例化、处理默认值等,但您可能会明白.

May be augmented to work with non NIB instantiations, handling default values, etc, but you probably get the idea.

这篇关于带有自定义字体的 UITextField secureTextEntry 项目符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆