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 - 拆分 Drawable 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01