-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIPDFMacSecureTextField.m
96 lines (75 loc) · 2.15 KB
/
IPDFMacSecureTextField.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// IPDFMacSecureTextField.m
// InstaPDF for Mac
//
// Created by mmackh on 20.10.19.
// Copyright © 2019 mackh ag. All rights reserved.
//
#import "IPDFMacSecureTextField.h"
@interface IPDFMacSecureTextField ()
@property (nonatomic) UIFont *backupFont;
@end
@implementation IPDFMacSecureTextField
- (instancetype)init
{
self = [super init];
if (!self) return nil;
return self;
}
- (void)setSecureTextEntryWithoutAutofill:(BOOL)secureTextEntryWithoutAutofill
{
_secureTextEntryWithoutAutofill = secureTextEntryWithoutAutofill;
if (secureTextEntryWithoutAutofill)
{
self.spellCheckingType = UITextSpellCheckingTypeNo;
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.accessibilityValue = @"Password";
return;
}
self.accessibilityValue = nil;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
if (![font isEqual:[self monoFont]])
{
_backupFont = font;
}
}
- (UIFont *)monoFont
{
return [UIFont monospacedSystemFontOfSize:16 weight:UIFontWeightSemibold];
}
- (UIFont *)currentFont
{
return (self.text.length) ? [self monoFont] : _backupFont;
}
- (void)drawTextInRect:(CGRect)rect
{
if (!_secureTextEntryWithoutAutofill)
{
[super drawTextInRect:rect];
return;
}
self.font = [self currentFont];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.minimumLineHeight = rect.size.height - 5;
NSDictionary *attributes = @{ NSFontAttributeName: self.font, NSParagraphStyleAttributeName: paragraphStyle };
NSMutableString *dotsMutable = [NSMutableString new];
NSInteger textLength = self.text.length;
while (textLength --> 0)
{
[dotsMutable appendString:@"•"];
}
[dotsMutable drawInRect:rect withAttributes:attributes];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (_secureTextEntryWithoutAutofill)
{
if (action == @selector(copy:)) return NO;
if (action == @selector(cut:)) return NO;
}
return [super canPerformAction:action withSender:sender];
}
@end