我在UITextField
中使用自定义字体,同时打开了secureTextEntry
。当我在单元格中输入时,我可以看到我选择的字体中的黑点符号,但是当字段失去焦点时,这些黑点符号会回归系统标准字体。如果我再次点击该字段,则它们会重新变回我的字体,以此类推。
是否有一种方法可以确保即使在字段失去焦点时也继续显示自定义字体的黑点符号?
我在UITextField
中使用自定义字体,同时打开了secureTextEntry
。当我在单元格中输入时,我可以看到我选择的字体中的黑点符号,但是当字段失去焦点时,这些黑点符号会回归系统标准字体。如果我再次点击该字段,则它们会重新变回我的字体,以此类推。
是否有一种方法可以确保即使在字段失去焦点时也继续显示自定义字体的黑点符号?
UITextField
,然后在IB中通过KVC将secure
属性设置为YES
。text
访问器中进行一些 hack,以始终获取该字段保存的实际文本。@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实例化,处理默认值等,但您可能已经理解了。
UITextFields
开始切换。 - Geri Borbás对于那些在切换secureTextEntry时遇到自定义字体丢失问题的人,我发现了一个解决方法(我正在使用iOS 8.4 SDK)。我试图为UITextField创建一个显示/隐藏密码的切换。每次我切换secureTextEntry = NO
时,我的自定义字体都会出错,只有最后一个字符显示了正确的字体。这方面肯定出了点问题,但是这是我的解决方案:
-(void)showPassword {
[self.textField resignFirstResponder];
self.textField.secureTextEntry = NO;
}
由于某些原因,第一响应者需要重新设计。当将secureTextEntry设置为YES时,似乎不需要重新设计第一响应者,只有在设置为NO时才需要。
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
rect.origin.y
来解决,但也许有更简洁的解决方案? - maxkonovalovrect.origin.y = floorf(rect.origin.y)
语句就会出现跳跃。使用 roundf()
可能比使用 floorf()
更好。 - Austin[passWordTextField resignFirstResponder];
passWordTextField.secureTextEntry = !passWordTextField.secureTextEntry;
[passWordTextField becomeFirstResponder];
这是解决此错误的最快方法!
我发现了解决这个问题的诀窍。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField tag]== TAG_PASS || [textField tag]== TAG_CPASS)
{
// To fix password dot size
if ([[textField text] isEqualToString:@"" ])
{
[textField setText:@" "];
[textField resignFirstResponder];
[textField becomeFirstResponder];
[textField setText:@""];
}
}
}
可以完全避免使用secureTextEntry
文本字段:
NSString *pin = @"";
BOOL pasting = FALSE;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(!pasting) {
pin = [pin stringByReplacingCharactersInRange:range withString:string];
// Bail out when deleting a character
if([string length] == 0) {
return YES;
}
pasting = TRUE;
[textField paste:@"●"];
return NO;
} else {
pasting = FALSE;
return YES;
}
}
Swift 5和iOS 14已经发布,但是对于自定义字体,即使将isSecureTextEntry设置为true,仍然会显示错误大小的圆点,尽管实际上前导字母的大小是正确的。
除了一种hacky的解决方法——在密码处于安全模式时将字体设置为系统字体之外,我从stackoverflow上找到的所有解决方案都没有起作用。
if textField.isSecureTextEntry {
self.textField.font = UIFont.systemFont(ofSize: 17)
} else {
self.textField.font = UIFont(name: "Roboto-Regular", size: 17)
}
iOS在使用自定义字体时有些奇怪。尝试为该文本字段取消“自适应大小”选项。如果这不起作用,我猜你困扰的是字体大小的增加。
一个简单的解决方案是:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:@"Helvetica-Bold" size:10.0]];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:@"Helvetica-Bold" size:10.0]];
}
}
为了让UITextField
失去焦点时看起来没有大小变化,您需要稍微调整一下大小。
如果您在字符之间有严重的间距问题,就像编辑后的问题一样,最简单(但有点丑陋)的解决方案是创建一个符合上述大小和间距的子弹图像,并与用户输入的字符数量相匹配,当用户离开UITextField
时将显示这些字符。
UITextField
有一个委托,并且上述方法正在被调用。 - SegevdidEndEditing
点关闭 secureTextEntry
,然后创建一个包含正确数量的圆点的字符串,将其设置为密码框的文本,并单独跟踪实际密码,根据需要进行交换。 - Luke