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

查看:19
本文介绍了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天全站免登陆