UITextField使用自定义字体的secureTextEntry项目符号?

Luk*_*uke 39 cocoa-touch objective-c uitextfield uikit ios

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

有没有办法可以确保他们继续显示自定义字体的项目符号,即使字段没有焦点?

在此输入图像描述 在此输入图像描述

Ger*_*eri 17

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

实际上它实现了lukech建议的评论.当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
Run Code Online (Sandbox Code Playgroud)

可以扩充以使用非NIB实例化,处理默认值等,但您可能会得到这个想法.

  • 我在UITextField上创建了类别来解决这个问题:https://github.com/elegion/ELFixSecureTextFieldFont(根据你的答案) (2认同)

小智 11

对于那些在切换secureTextEntry时丢失自定义字体有困难的人,我找到了解决办法(我使用的是iOS 8.4 SDK).我试图在UITextField中显示/隐藏密码.每次我切换secureTextEntry = NO我的自定义字体都被borked,只有最后一个字符显示正确的字体.时髦的东西肯定会继续下去,但这是我的解决方案:

-(void)showPassword {
    [self.textField resignFirstResponder];
    self.textField.secureTextEntry = NO;
}
Run Code Online (Sandbox Code Playgroud)

第一响应者需要因某种原因而辞职.将secureTextEntry设置为YES时,您似乎不需要重新设置第一个响应者,仅当设置为NO时.


Aus*_*tin 9

实际问题似乎是编辑视图(编辑时UITextField 绘制自己的文本)使用项目符号(U + 2022)绘制编辑后的字符,而UITextField使用黑色圆圈(U + 25CF).我想在默认字体中,这些字符看起来是一样的.

对于任何感兴趣的人来说,这是一个替代解决方法,它使用自定义文本字段子类,但不需要处理文本属性或其他特殊配置.国际海事组织,这使事情相对干净.

@interface MyTextField : UITextField
@end

@implementation MyTextField

- (void)drawTextInRect:(CGRect)rect
{
    if (self.isSecureTextEntry)
    {
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.alignment = self.textAlignment;

        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:self.font forKey:NSFontAttributeName];
        [attributes setValue:self.textColor forKey:NSForegroundColorAttributeName];
        [attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];

        CGSize textSize = [self.text sizeWithAttributes:attributes];

        rect = CGRectInset(rect, 0, (CGRectGetHeight(rect) - textSize.height) * 0.5);
        rect.origin.y = floorf(rect.origin.y);

        NSMutableString *redactedText = [NSMutableString new];
        while (redactedText.length < self.text.length)
        {
            [redactedText appendString:@"\u2022"];
        }

        [redactedText drawInRect:rect withAttributes:attributes];
    }
    else
    {
        [super drawTextInRect:rect];
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 永远不会调用drawTextInRect方法. (3认同)