c_cpp 多行占位符文本视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 多行占位符文本视图相关的知识,希望对你有一定的参考价值。
#import "LCTextView.h"
@interface LCTextView()
@property (strong, nonatomic) UILabel *placeholderLabel;
@end
@implementation LCTextView
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.placeholderLabel = nil;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder]){
self.contentOffset = CGPointZero;
self.contentInset = UIEdgeInsetsZero;
self.textContainerInset = UIEdgeInsetsMake(0,0,0,0);
self.textContainer.lineFragmentPadding = 0;
UILabel *placeholderLabel = [[UILabel alloc] init];
placeholderLabel.translatesAutoresizingMaskIntoConstraints = NO;
placeholderLabel.font = self.font;
placeholderLabel.numberOfLines = 0;
self.placeholderLabel = placeholderLabel;
[self addSubview:placeholderLabel];
NSArray *H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label(==textView)]|" options:0 metrics:nil views:@{@"label": placeholderLabel, @"textView": self}];
NSArray *V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]" options:0 metrics:nil views:@{@"label": placeholderLabel}];
NSArray *constaints = [H arrayByAddingObjectsFromArray:V];
[self addConstraints:constaints];
[self _updatePlaceholderLabelUI:placeholderLabel text:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangeNotifiaciton:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)setText:(NSString *)text
{
if([self.text isEqualToString:text]){
return;
}
[self _updatePlaceholderLabelUI:self.placeholderLabel text:text];
[super setText:text];
}
- (void)setPlaceholder:(NSString *)placeholder
{
self.label.text = placeholder;
[self _updatePlaceholderLabelUI:self.placeholderLabel text:self.text];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
self.label.textColor = placeholderColor;
}
- (void)_updatePlaceholderLabelUI:(UILabel *)label text:(NSString *)text
{
label.hidden = ![text isEqualToString:@""];
}
#pragma mark - Notification
- (void)textChangeNotifiaciton:(NSNotification *)n
{
[self _updatePlaceholderLabelUI:self.placeholderLabel text:self.text];
}
@end
@interface LCTextView : UITextView
@property (strong, nonatomic) NSString *placeholder;
@property (strong, nonatomic) UIColor *placeholderColor;
- (void)textChangeNotifiaciton:(NSNotification *)n;
@end
以上是关于c_cpp 多行占位符文本视图的主要内容,如果未能解决你的问题,请参考以下文章
打造一个支持占位符的多行文本输入框
是否可以在文本框 ASP.NET 中使用多行占位符?
占位符文本在(多行)TextInput for Android(React Native)中的位置
在 HTML5 中不允许使用多行占位符的理由是啥?
如何在一个视图控制器中快速将占位符设置为多个文本视图
将占位符添加到表格视图单元格上的文本字段