Skip to content

Commit 24ba914

Browse files
committed
[new] Set placeholderColor in its own generic class
1 parent 6668c8b commit 24ba914

File tree

4 files changed

+92
-19
lines changed

4 files changed

+92
-19
lines changed

Classes/MPNumericTextField.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ IB_DESIGNABLE
4444
@interface MPNumericTextField : UITextField
4545

4646
@property (nonatomic, copy) NSString *encodedValue;
47-
@property (nonatomic, strong) IBInspectable UIColor *placeholderColor;
4847
@property (nonatomic, assign) MPNumericTextFieldType type;
4948
@property (nonatomic, strong) NSLocale *locale;
5049
@property (nonatomic, copy) NSString *currencyCode;

Classes/MPNumericTextField.m

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,6 @@ - (void) setType:(MPNumericTextFieldType)type {
103103
}
104104
}
105105

106-
- (void)drawPlaceholderInRect:(CGRect)rect
107-
{
108-
if (self.placeholderColor != nil) {
109-
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
110-
111-
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
112-
style.lineBreakMode = NSLineBreakByTruncatingTail;
113-
style.alignment = self.textAlignment;
114-
115-
NSDictionary *attributes = @{NSFontAttributeName: self.font,
116-
NSParagraphStyleAttributeName: style,
117-
NSForegroundColorAttributeName: self.placeholderColor};
118-
[self.placeholder drawInRect:placeholderRect withAttributes:attributes];
119-
} else {
120-
[super drawPlaceholderInRect:rect];
121-
}
122-
}
123-
124106
- (void)setEncodedValue:(NSString *)encodedValue
125107
{
126108
_encodedValue = encodedValue;

Classes/MPTextField.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// MPTextField.h
3+
//
4+
// Version 1.4.0
5+
//
6+
// Created by Daniele Di Bernardo on 01/04/17.
7+
// Copyright (c) 2017 marzapower. All rights reserved.
8+
//
9+
// The MIT License (MIT)
10+
//
11+
// Copyright (c) [2014-2017] [Daniele Di Bernardo]
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
#import <UIKit/UIKit.h>
33+
34+
@interface MPTextField : UITextField
35+
36+
@property (nonatomic, strong) IBInspectable UIColor *placeholderColor;
37+
38+
@end

Classes/MPTextField.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// MPTextField.h
3+
//
4+
// Version 1.4.0
5+
//
6+
// Created by Daniele Di Bernardo on 01/04/17.
7+
// Copyright (c) 2017 marzapower. All rights reserved.
8+
//
9+
// The MIT License (MIT)
10+
//
11+
// Copyright (c) [2014-2017] [Daniele Di Bernardo]
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all
21+
// copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29+
// SOFTWARE.
30+
//
31+
32+
#import "MPTextField.h"
33+
34+
@implementation MPTextField
35+
36+
- (void)drawPlaceholderInRect:(CGRect)rect
37+
{
38+
if (self.placeholderColor != nil) {
39+
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
40+
41+
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
42+
style.lineBreakMode = NSLineBreakByTruncatingTail;
43+
style.alignment = self.textAlignment;
44+
45+
NSDictionary *attributes = @{NSFontAttributeName: self.font,
46+
NSParagraphStyleAttributeName: style,
47+
NSForegroundColorAttributeName: self.placeholderColor};
48+
[self.placeholder drawInRect:placeholderRect withAttributes:attributes];
49+
} else {
50+
[super drawPlaceholderInRect:rect];
51+
}
52+
}
53+
54+
@end

0 commit comments

Comments
 (0)