UITextField secureTextEntry bullets with a custom font?(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 带有自定义字体的项目符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UITextField secureTextEntry 带有自定义字体的项目符号?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01