Files
featherVoice/QXLive/Tools/QXTextView.m

150 lines
4.6 KiB
Mathematica
Raw Normal View History

2025-08-08 10:49:36 +08:00
//
// QXTextView.m
// QXLive
//
// Created by on 2025/5/15.
//
#import "QXTextView.h"
@interface QXTextView()<UITextViewDelegate>
@property (nonatomic,strong)UILabel *maxCountLabel;
@end
@implementation QXTextView
- (instancetype)init
{
self = [super init];
if (self) {
[self initSubview];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubview];
}
return self;
}
-(void)initSubview{
2025-10-20 09:43:10 +08:00
self.lengthColor = QXConfig.themeColor;
2025-08-08 10:49:36 +08:00
self.maxLength = 120;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
self.delegate = self;
[self addSubview:self.placehoulderLabel];
self.returnKeyType = UIReturnKeyDone;
[self addSubview:self.maxCountLabel];
}
-(void)layoutSubviews{
[super layoutSubviews];
self.placehoulderLabel.frame = CGRectMake(10, 10, self.width-20, 50);
[self.placehoulderLabel sizeToFit];
self.maxCountLabel.frame = CGRectMake(10, self.height-20, self.width-20, 15);
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
if (textView.text.length == 0) {
self.placehoulderLabel.hidden = NO;
}else{
self.placehoulderLabel.hidden = YES;
}
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder];
return NO;
}
return YES;
}
-(void)textViewDidChange:(UITextView*)textView{
if (self.text.length == 0) {
self.placehoulderLabel.hidden = NO;
}else{
self.placehoulderLabel.hidden = YES;
}
if (self.text.length > self.maxLength) {
self.text = [self.text substringToIndex:self.maxLength];
return;
}
if (self.maxLengthHidden) {
return;
}
NSString *length = [NSString stringWithFormat:@"%ld",self.maxLength - self.text.length];
NSString *str = [NSString stringWithFormat:@"%@%@%@",QXText(@"还可输入"),length,QXText(@"字")];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
[attr yy_setColor:self.lengthColor range:[str rangeOfString:length]];
self.maxCountLabel.attributedText = attr;
}
-(void)setMaxLength:(NSInteger)maxLength{
_maxLength = maxLength;
NSString *length = [NSString stringWithFormat:@"%ld",self.maxLength - self.text.length];
NSString *str = [NSString stringWithFormat:@"%@%@%@",QXText(@"还可输入"),length,QXText(@"字")];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
[attr yy_setColor:self.lengthColor range:[str rangeOfString:length]];
self.maxCountLabel.attributedText = attr;
}
-(void)setMaxLengthHidden:(BOOL)maxLengthHidden{
_maxLengthHidden = maxLengthHidden;
self.maxCountLabel.hidden = YES;
}
-(void)setPlacehoulderColor:(UIColor *)placehoulderColor{
_placehoulderColor = placehoulderColor;
self.placehoulderLabel.textColor = placehoulderColor;
}
-(void)setPlacehoulder:(NSString *)placehoulder{
_placehoulder = placehoulder;
self.placehoulderLabel.text = placehoulder;
}
-(void)setPlacehoulderFont:(UIFont *)placehoulderFont{
_placehoulderFont = placehoulderFont;
self.placehoulderLabel.font = placehoulderFont;
}
-(void)setLengthColor:(UIColor *)lengthColor{
_lengthColor = lengthColor;
}
-(UILabel *)placehoulderLabel{
if (!_placehoulderLabel) {
_placehoulderLabel = [[UILabel alloc] init];
_placehoulderLabel.numberOfLines = 0;
_placehoulderLabel.font = [UIFont systemFontOfSize:12.f];
_placehoulderLabel.textColor = RGB16(0x9B9B9B);
MJWeakSelf
[_placehoulderLabel addTapBlock:^(id _Nonnull obj) {
[weakSelf becomeFirstResponder];
}];
}
return _placehoulderLabel;
}
-(UILabel *)maxCountLabel{
if (!_maxCountLabel) {
_maxCountLabel = [[UILabel alloc] init];
_maxCountLabel.font = [UIFont systemFontOfSize:9];
_maxCountLabel.textAlignment = NSTextAlignmentRight;
_maxCountLabel.textColor = RGB16(0x666666);
NSString *length = [NSString stringWithFormat:@"%ld",self.maxLength - self.text.length];
NSString *str = [NSString stringWithFormat:@"%@%@%@",QXText(@"还可输入"),length,QXText(@"字")];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
[attr yy_setColor:self.lengthColor range:[str rangeOfString:length]];
_maxCountLabel.attributedText = attr;
}
return _maxCountLabel;
}
@end