Ios UITextField secureTextEntry项目符号是否具有自定义字体?
我正在Ios UITextField secureTextEntry项目符号是否具有自定义字体?,ios,objective-c,cocoa-touch,uikit,uitextfield,Ios,Objective C,Cocoa Touch,Uikit,Uitextfield,我正在UITextField中使用自定义字体,该字段已启用secureTextEntry。当我在单元格中键入时,我会看到所选字体中的项目符号,但当字段失去焦点时,这些项目符号会恢复为系统标准字体。如果我再次点击该字段,它们将变回我的字体,依此类推 有没有一种方法可以确保它们继续显示自定义字体的项目符号,即使字段不对焦 iOS在定制字体方面表现得有点奇怪。尝试删除该文本字段的“调整以适应”。如果这不起作用,我猜困扰你的是字体大小的增加 一个简单的解决方案是: - (void)textFieldD
UITextField
中使用自定义字体,该字段已启用secureTextEntry
。当我在单元格中键入时,我会看到所选字体中的项目符号,但当字段失去焦点时,这些项目符号会恢复为系统标准字体。如果我再次点击该字段,它们将变回我的字体,依此类推
有没有一种方法可以确保它们继续显示自定义字体的项目符号,即使字段不对焦
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
时显示的用户输入的字符量匹配。虽然这是一个iOS错误(我应该补充,是iOS 7中的新错误),但我有另一种方法来解决它,人们可能会觉得可以接受。功能仍然略有下降,但降幅不大
基本上,只要字段中输入了内容,就将字体设置为默认字体系列/样式;但如果未输入任何内容,请将其设置为自定义字体。(字体大小可以单独设置,因为它是族/样式,而不是大小,这是错误的。)捕获字段值的每次更改,并在此时相应地设置字体。然后,当没有输入任何内容时,微弱的“提示”文本具有您想要的字体(自定义);但当输入任何内容时(无论您是否正在编辑),都将使用默认值(Helvetica)。既然子弹就是子弹,这看起来应该没问题
一个缺点是,在被项目符号替换之前键入的字符将使用默认字体(Helvetica)。不过,这只是每个角色的一瞬间。如果这是可以接受的,那么这个解决方案是有效的。解决这个问题的子类。创建一个任意
UITextField
,然后将secure
属性设置为YES
(通过IB中的KVC)
实际上,它实现了由建议的注释。当textfield结束编辑时,它会切换到一个任意的textfield,然后在其中设置大量的点,并在text
accessor中进行一些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
@interface securetextfield with customfont:UITextField
@财产(非原子)布尔安全;
@属性(非原子,强)NSString*actualText;
@结束
@实现SecureTextFieldWithCustomFont
-(无效)从NIB中唤醒
{
[超级awakeFromNib];
if(self.secureTextEntry)
{
//倾听变化。
[自添加目标:自操作:@selector(editingdidbeagin)for controlEvents:uicontrolEventeditingdidbeagin];
[自添加目标:自操作:@selector(editingDidChange)for controlEvents:UIControlEventEditingChanged];
[自我添加目标:自我操作:@selector(editingDidFinish)for ControlEvents:UIControlEventEditingDidEnd];
}
}
-(NSString*)文本
{
if(self.editing | | self.secure==否)
{返回[超级文本];}
其他的
{返回self.actualText;}
}
-(无效)编辑开始
{
self.secureTextEntry=是;
self.text=self.actualText;
}
-(作废)编辑更改
{self.actualText=self.text;}
-(无效)编辑完成
{
self.secureTextEntry=否;
self.actualText=self.text;
self.text=[self-dotpocholder];
}
-(NSString*)点占位符
{
int指数=0;
NSMutableString*dots=@“。mutableCopy;
while(索引
可能会被扩展以处理非NIB实例化、处理默认值等,但您可能会明白这一点。实际的问题似乎是编辑视图(
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
@interface MyTextField:UITextField
@结束
@实现MyTextField
-(void)drawTextInRect:(CGRect)rect
{
if(self.issecurentextry)
{
NSMutableParagraphStyle*paragraphStyle=[NSMutableParagraphStyle新建];
paragraphStyle.alignment=self.textAlignment;
NSMutableDictionary*属性=[NSMutableDictionary];
[属性集合值:self.font-forKey:NSFontAttributeName];
[属性集值:self.textColor-forKey:NSForegroundColorAttributeName];
[attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
CGSize textSize=[self.text size属性:属性];
rect=CGRectInset(rect,0,(CGRectGetHeight(rect)-textSize.height)*0.5);
rect.origin.y=地板(rect.origin.y);
NSMutableString*redactedText=[NSMutableString new];
while(redactedText.length
我找到了解决这个问题的窍门
- (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时无法丢失自定义字体的用户,我找到了一个解决方法(我使用的是iOS 8.4 SDK)。我尝试切换显示/隐藏
-(void)showPassword {
[self.textField resignFirstResponder];
self.textField.secureTextEntry = NO;
}
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;
}
}
[passWordTextField resignFirstResponder];
passWordTextField.secureTextEntry = !passWordTextField.secureTextEntry;
[passWordTextField becomeFirstResponder];
if textField.isSecureTextEntry {
self.textField.font = UIFont.systemFont(ofSize: 17)
} else {
self.textField.font = UIFont(name: "Roboto-Regular", size: 17)
}