Commit 80eb68d7 cgx

优化输入框占位文字显示问题

1 个父辈 6e0740a2
...@@ -174,7 +174,7 @@ ...@@ -174,7 +174,7 @@
_textView.delegate = self; _textView.delegate = self;
[_textView cornerRadius:12]; [_textView cornerRadius:12];
_textView.textContainerInset = UIEdgeInsetsMake(15, 8, 40, 8); _textView.textContainerInset = UIEdgeInsetsMake(15, 8, 40, 8);
_textView.placeholder = @"请文明发言,不可涉及黄、赌、毒、国家政治相关动态!"; _textView.placeholder = @"请文明发言,不可发布涉及黄、赌、毒、国家政治相关动态!";
_textView.placeholderLabel.dk_textColorPicker = DKColorPickerWithColors(ColorFromHexA(0x777777, .3), ColorFromHexA(0xFFFFFF, .3), DSWhite); _textView.placeholderLabel.dk_textColorPicker = DKColorPickerWithColors(ColorFromHexA(0x777777, .3), ColorFromHexA(0xFFFFFF, .3), DSWhite);
} }
return _textView; return _textView;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
@interface MKPPlaceholderTextView : UITextView @interface MKPPlaceholderTextView : UITextView
/** 占位文字label */ /** 占位文字label */
@property(nonatomic, weak) UILabel *placeholderLabel; @property(nonatomic, strong) UILabel *placeholderLabel;
/** 占位文字 */ /** 占位文字 */
@property(nonatomic, copy) NSString *placeholder; @property(nonatomic, copy) NSString *placeholder;
......
...@@ -9,34 +9,25 @@ ...@@ -9,34 +9,25 @@
#import "MKPPlaceholderTextView.h" #import "MKPPlaceholderTextView.h"
@interface MKPPlaceholderTextView () @interface MKPPlaceholderTextView ()
@property (nonatomic, strong) UIView *tmpView;
@end @end
@implementation MKPPlaceholderTextView @implementation MKPPlaceholderTextView
- (UILabel *)placeholderLabel {
if(!_placeholderLabel) {
// 添加一个用来显示占位文字的label
UILabel *placeholderLabel = [[UILabel alloc]init];
placeholderLabel.numberOfLines = 0;
placeholderLabel.x = 4;
placeholderLabel.y = 7;
[self addSubview:placeholderLabel];
_placeholderLabel = placeholderLabel;
}
return _placeholderLabel;
}
- (instancetype)initWithFrame:(CGRect)frame { - (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) { if (self = [super initWithFrame:frame]) {
// 垂直方向上永远有弹簧效果
//self.alwaysBounceVertical = YES;
// 默认字体 // 默认字体
self.font = [UIFont systemFontOfSize:15]; self.font = [UIFont systemFontOfSize:15];
// 默认的占位文字颜色 // 默认的占位文字颜色
self.placeholderColor = ColorFromHexA(0xFFFFFF, .6); self.placeholderColor = ColorFromHexA(0xFFFFFF, .6);
[self addSubview:self.tmpView];
[self.tmpView addSubview:self.placeholderLabel];
[self.tmpView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
make.size.equalTo(self);
}];
// 监听文字改变 // 监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
} }
...@@ -47,6 +38,18 @@ ...@@ -47,6 +38,18 @@
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }
- (void)layoutSubviews {
[self.placeholderLabel sizeToFit];
UIEdgeInsets insets = self.textContainerInset;
CGFloat leftMargin = insets.left == 0 ? 8 : insets.left + 3;
CGFloat topMargin = insets.top == 0 ? 8 : insets.top;
[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.tmpView).offset(leftMargin);
make.top.equalTo(self.tmpView).offset(topMargin);
make.right.equalTo(self.tmpView).offset(-leftMargin);
}];
}
/** /**
* 监听文字改变 * 监听文字改变
*/ */
...@@ -55,19 +58,6 @@ ...@@ -55,19 +58,6 @@
self.placeholderLabel.hidden = self.hasText; self.placeholderLabel.hidden = self.hasText;
} }
/**
* 更新占位文字的尺寸
*/
- (void)layoutSubviews {
[super layoutSubviews];
UIEdgeInsets insets = self.textContainerInset;
self.placeholderLabel.x = insets.left == 0 ? 8 : insets.left + 3;
self.placeholderLabel.y = insets.top == 0 ? 8 : insets.top;
self.placeholderLabel.width = self.width - 2 * self.placeholderLabel.x;
[self.placeholderLabel sizeToFit];
}
#pragma mark - 重写setter #pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor { - (void)setPlaceholderColor:(UIColor *)placeholderColor {
_placeholderColor = placeholderColor; _placeholderColor = placeholderColor;
...@@ -100,4 +90,21 @@ ...@@ -100,4 +90,21 @@
[self textDidChange]; [self textDidChange];
} }
#pragma mark - lazy
- (UIView *)tmpView {
if (!_tmpView) {
_tmpView = [UIView new];
_tmpView.userInteractionEnabled = NO;
}
return _tmpView;
}
- (UILabel *)placeholderLabel {
if(!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.numberOfLines = 0;
}
return _placeholderLabel;
}
@end @end
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!