You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.7 KiB
114 lines
3.7 KiB
// |
|
// TFTextView.m |
|
// TFReader |
|
// |
|
// Created by 谢腾飞 on 2020/12/23. |
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved. |
|
// |
|
|
|
#import "TFTextView.h" |
|
#import <YYKit.h> |
|
|
|
@interface TFTextView ()<YYTextViewDelegate> |
|
|
|
@property (nonatomic ,strong) YYTextView *textView; |
|
@property (nonatomic ,strong) UILabel *countLabel; |
|
@property (nonatomic ,strong) NSString *textStr; |
|
|
|
@end |
|
|
|
@implementation TFTextView |
|
|
|
- (instancetype)initWithFrame:(CGRect)frame |
|
{ |
|
if (self = [super initWithFrame:frame]) { |
|
|
|
self.layer.borderColor = kGrayViewColor.CGColor; |
|
self.layer.borderWidth = 0.6; |
|
self.backgroundColor = [UIColor whiteColor]; |
|
|
|
[self createSubViews]; |
|
} |
|
return self; |
|
} |
|
|
|
- (void)createSubViews |
|
{ |
|
self.textView = [[YYTextView alloc] initWithFrame:CGRectZero]; |
|
self.textView.placeholderText = TFLocalizedString(@"说说你的看法..."); |
|
self.textView.placeholderFont = kFont13; |
|
self.textView.placeholderTextColor = [UIColor grayColor]; |
|
self.textView.font = kMainFont; |
|
self.textView.textColor = kBlackColor; |
|
self.textView.delegate = self; |
|
[self addSubview:self.textView]; |
|
|
|
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.left.mas_equalTo(self.mas_left).with.offset(5); |
|
make.top.mas_equalTo(self.mas_top).with.offset(5); |
|
make.width.mas_equalTo(self.mas_width).with.offset(- 10); |
|
make.height.mas_equalTo(self.mas_height).with.offset(- 20); |
|
}]; |
|
|
|
|
|
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)]; |
|
toolBar.barStyle = UIBarStyleDefault; |
|
UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; |
|
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:TFLocalizedString(@"完成") style:UIBarButtonItemStyleDone target:self action:@selector(hideKeyboard)]; |
|
[toolBar setItems:@[flexibleSpaceItem, doneItem]]; |
|
self.textView.inputAccessoryView = toolBar; |
|
|
|
|
|
self.countLabel = [[UILabel alloc] init]; |
|
self.countLabel.font = kFont10; |
|
self.countLabel.textColor = kGrayTextColor; |
|
self.countLabel.hidden = YES; |
|
self.countLabel.textAlignment = NSTextAlignmentRight; |
|
[self addSubview:self.countLabel]; |
|
|
|
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.left.mas_equalTo(self.textView.mas_left); |
|
make.bottom.mas_equalTo(self.mas_bottom).with.offset(-5); |
|
make.width.mas_equalTo(self.textView.mas_width); |
|
make.height.mas_equalTo(10); |
|
}]; |
|
} |
|
|
|
- (void)setMaxWordCount:(NSUInteger)maxWordCount |
|
{ |
|
_maxWordCount = maxWordCount; |
|
if (maxWordCount > 0) { |
|
self.countLabel.hidden = NO; |
|
self.countLabel.text = [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:0], [TFUtilsHelper formatStringWithInteger:_maxWordCount]]; |
|
} |
|
} |
|
|
|
- (NSString *)text |
|
{ |
|
return [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""]; |
|
} |
|
|
|
- (void)resetPlaceholderText:(NSString *)placeholderText |
|
{ |
|
self.textView.placeholderText = placeholderText; |
|
} |
|
|
|
- (void)textViewDidChange:(YYTextView *)textView |
|
{ |
|
if (textView.text.length >= _maxWordCount) { |
|
textView.text = [textView.text substringToIndex:_maxWordCount]; |
|
} |
|
|
|
if ([self.delegate respondsToSelector:@selector(textViewDidChange:)]) { |
|
[self.delegate textViewDidChange:textView.text]; |
|
} |
|
|
|
self.countLabel.text = [NSString stringWithFormat:@"%@/%@", [TFUtilsHelper formatStringWithInteger:textView.text.length], [TFUtilsHelper formatStringWithInteger:_maxWordCount]]; |
|
} |
|
|
|
- (void)hideKeyboard |
|
{ |
|
[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; |
|
} |
|
|
|
@end
|
|
|