我在用UITextView的时候,默认是不能设置placeholder的,于是网上找了一些资料,最后找到了一个别人写的自定义类,拿来用用,顺便分享一下。
CPTextViewPlaceholder.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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 #import "CPTextViewPlaceholder.h" @interface CPTextViewPlaceholder ()@property (nonatomic ) UITextAutocorrectionType originalCorrection;@property (nonatomic , strong ) UIColor *placeholderColor;@property (nonatomic , strong ) UIColor *originalTextColor;@property (nonatomic , getter = isUsingPlaceholder) BOOL usingPlaceholder;@property (nonatomic , getter = isSettingPlaceholder) BOOL settingPlaceholder;@end @implementation CPTextViewPlaceholder #pragma mark - #pragma mark Life Cycle method - (id )initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { self .placeholderColor = [UIColor lightGrayColor]; self .originalCorrection = self .autocorrectionType; self .originalTextColor = super .textColor; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (didBeginEditing) name:UITextViewTextDidBeginEditingNotification object:nil ]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (didEndEditing) name:UITextViewTextDidEndEditingNotification object:nil ]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (textDidChange:) name:UITextViewTextDidChangeNotification object:self ]; } return self ; } - (void )dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:self ]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:self ]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self ]; } - (void )layoutSubviews { [super layoutSubviews]; if ([UIDevice currentDevice].systemVersion.floatValue < 6.000000 ) { if (self .isUsingPlaceholder && self .isFirstResponder) { self .text = @"" ; } } } #pragma mark - #pragma mark Notifications - (void )didBeginEditing { if (self .isUsingPlaceholder) { [self sendCursorToBeginning]; } } - (void )didEndEditing { if (self .text.length == 0 ) { [self setupPlaceholder]; } } - (void )textDidChange:(NSNotification *)notification { if (self .isSettingPlaceholder) { return ; } if (self .text.length == 0 ) { [self setupPlaceholder]; return ; } if (self .isUsingPlaceholder) { self .usingPlaceholder = NO ; NSRange range = [self .text rangeOfString:self .placeholder options:NSLiteralSearch ]; if (range.location != NSNotFound ) { NSString *newText = [self .text stringByReplacingCharactersInRange:range withString:@"" ]; super .textColor = self .originalTextColor; super .autocorrectionType = self .originalCorrection; if ([newText isEqualToString:self .placeholder]) { [self sendCursorToEnd]; } else if (newText.length == 0 ) { [self setupPlaceholder]; return ; } self .text = newText; } } } - (BOOL )canPerformAction:(SEL)action withSender:(id )sender { if (self .isUsingPlaceholder && action != @selector (paste:)) { return NO ; } return [super canPerformAction:action withSender:sender]; } #pragma mark - Getters and Setters - (void )setAutocorrectionType:(UITextAutocorrectionType )autocorrectionType { [super setAutocorrectionType:autocorrectionType]; self .originalCorrection = autocorrectionType; } - (void )setPlaceholder:(NSString *)placeholder { _placeholder = placeholder; if (self .isUsingPlaceholder || self .text.length == 0 ) { [self setupPlaceholder]; } } - (void )setTextColor:(UIColor *)textColor { [super setTextColor:textColor]; self .originalTextColor = textColor; } - (void )setSelectedRange:(NSRange )selectedRange { if (self .isUsingPlaceholder) { [self sendCursorToBeginning]; } else { [super setSelectedRange:selectedRange]; } } - (void )setSelectedTextRange:(UITextRange *)selectedTextRange { if (self .isUsingPlaceholder) { [self sendCursorToBeginning]; } else { [super setSelectedTextRange:selectedTextRange]; } } #pragma mark - #pragma mark Utilities methods - (void )setupPlaceholder { super .autocorrectionType = UITextAutocorrectionTypeNo ; self .usingPlaceholder = YES ; self .settingPlaceholder = YES ; self .text = self .placeholder; self .settingPlaceholder = NO ; super .textColor = self .placeholderColor; [self sendCursorToBeginning]; } - (void )sendCursorToBeginning { [self performSelector:@selector (cursorToBeginning) withObject:nil afterDelay:0.01 ]; } - (void )cursorToBeginning { super .selectedRange = NSMakeRange (0 , 0 ); } - (void )sendCursorToEnd { [self performSelector:@selector (cursorToEnd) withObject:nil afterDelay:0.01 ]; } - (void )cursorToEnd { super .selectedRange = NSMakeRange (self .text.length, 0 ); } @end
CPTextViewPlaceholder.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #import <UIKit/UIKit.h> @interface CPTextViewPlaceholder : UITextView @property (nonatomic , strong ) NSString *placeholder;@end