172 lines
5.0 KiB
Mathematica
172 lines
5.0 KiB
Mathematica
|
|
//
|
|||
|
|
// FSTextInputView.m
|
|||
|
|
// FashionApp
|
|||
|
|
//
|
|||
|
|
// Created by 1 on 2018/5/25.
|
|||
|
|
// Copyright © 2018年 1. All rights reserved.
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
#import "FSTextInputView.h"
|
|||
|
|
#import "NSString+MTLimit.h"
|
|||
|
|
|
|||
|
|
@interface FSTextInputView() <UITextViewDelegate>
|
|||
|
|
|
|||
|
|
@end
|
|||
|
|
|
|||
|
|
@implementation FSTextInputView
|
|||
|
|
|
|||
|
|
- (instancetype)init
|
|||
|
|
{
|
|||
|
|
if (self = [super init]) {
|
|||
|
|
[self createInputview];
|
|||
|
|
}
|
|||
|
|
return self;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|||
|
|
{
|
|||
|
|
if(self = [super initWithFrame:frame])
|
|||
|
|
{
|
|||
|
|
[self createInputview];
|
|||
|
|
}
|
|||
|
|
return self;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)layoutSubviews
|
|||
|
|
{
|
|||
|
|
[super layoutSubviews];
|
|||
|
|
|
|||
|
|
_textview.frame = CGRectMake(self.padding.left, self.padding.top, self.width - self.padding.left - self.padding.right, self.height - self.padding.top - self.padding.bottom);
|
|||
|
|
|
|||
|
|
_placeHolderLabel.frame = _textview.bounds;
|
|||
|
|
_placeHolderLabel.x = _textview.x + 8;
|
|||
|
|
_placeHolderLabel.width = _textview.width - _placeHolderLabel.x;
|
|||
|
|
_placeHolderLabel.height = _textview.height;
|
|||
|
|
_placeHolderLabel.font = _textview.font;
|
|||
|
|
_placeHolderLabel.hidden = _textview.text.length > 0;
|
|||
|
|
// [self.placeHolderLabel sizeToFit];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)updateUI
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)createInputview
|
|||
|
|
{
|
|||
|
|
self.padding = UIEdgeInsetsMake(10, 10, 10, 10);
|
|||
|
|
_textview = [[UITextView alloc] initWithFrame:CGRectZero];
|
|||
|
|
_textview.showsVerticalScrollIndicator = NO;
|
|||
|
|
_textview.showsHorizontalScrollIndicator = NO;
|
|||
|
|
_textview.backgroundColor = [UIColor clearColor];
|
|||
|
|
_textview.scrollsToTop = NO;
|
|||
|
|
_textview.textColor = TITLE_COLOR;
|
|||
|
|
_textview.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
|
|||
|
|
_textview.layoutManager.allowsNonContiguousLayout = NO;
|
|||
|
|
_textview.font = MT_FONT_REGULAR_SIZE(15);
|
|||
|
|
[self addSubview:_textview];
|
|||
|
|
_textview.delegate = self;
|
|||
|
|
|
|||
|
|
self.maxTextLength = 14000;
|
|||
|
|
|
|||
|
|
// 控制textviw里面属性
|
|||
|
|
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
|
|||
|
|
paragraphStyle.lineSpacing = 5;// 字体的行间距 _textview高度太大,这个值太小会出问题
|
|||
|
|
// paragraphStyle.firstLineHeadIndent = 0.f; /**首行缩进宽度*/
|
|||
|
|
// paragraphStyle.alignment = NSTextAlignmentJustified;
|
|||
|
|
NSDictionary *attributes = @{
|
|||
|
|
NSParagraphStyleAttributeName:paragraphStyle
|
|||
|
|
};
|
|||
|
|
_textview.attributedText = [[NSAttributedString alloc] initWithString:_textview.text attributes:attributes];
|
|||
|
|
|
|||
|
|
// _placeHolderLabel
|
|||
|
|
_placeHolderLabel = [[UILabel alloc] init];
|
|||
|
|
_placeHolderLabel.font = _textview.font;
|
|||
|
|
_placeHolderLabel.textColor = COLOR_WITH_RGB(0xC7C7CD);
|
|||
|
|
_placeHolderLabel.x = 5;
|
|||
|
|
_placeHolderLabel.numberOfLines = 0;
|
|||
|
|
[_textview addSubview:_placeHolderLabel];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#pragma mark - UITextViewDelegate
|
|||
|
|
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
|||
|
|
{
|
|||
|
|
NSString *temstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
|
|||
|
|
temstr = temstr ? : @"";
|
|||
|
|
|
|||
|
|
if (temstr.length == 0) {
|
|||
|
|
_placeHolderLabel.hidden = NO;
|
|||
|
|
}else {
|
|||
|
|
_placeHolderLabel.hidden = YES;
|
|||
|
|
}
|
|||
|
|
UITextRange *selectedRange = [textView markedTextRange];
|
|||
|
|
// 获取高亮部分 中文联想
|
|||
|
|
UITextPosition *posi = [textView positionFromPosition:selectedRange.start offset:0];
|
|||
|
|
|
|||
|
|
// 如果在变化中是高亮部分在变,就不要计算字符
|
|||
|
|
if (selectedRange && posi) {
|
|||
|
|
return YES;
|
|||
|
|
}
|
|||
|
|
if (temstr.length > self.maxTextLength) {
|
|||
|
|
textView.text = [temstr mt_safeSubStringWithStringLength:self.maxTextLength];
|
|||
|
|
// 移动光标
|
|||
|
|
textView.selectedRange = NSMakeRange(textView.text.length, 0);
|
|||
|
|
|
|||
|
|
if ([self.delegate respondsToSelector:@selector(didChangeText:)]) {
|
|||
|
|
[self.delegate didChangeText:self];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return NO;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return YES;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)textViewDidChange:(UITextView *)textView
|
|||
|
|
{
|
|||
|
|
UITextRange *selectedRange = [textView markedTextRange];
|
|||
|
|
// 获取高亮部分 中文联想
|
|||
|
|
UITextPosition *posi = [textView positionFromPosition:selectedRange.start offset:0];
|
|||
|
|
|
|||
|
|
// 如果在变化中是高亮部分在变,就不要计算字符
|
|||
|
|
if (selectedRange && posi) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (textView.text.length > self.maxTextLength) {
|
|||
|
|
textView.text = [textView.text mt_safeSubStringWithStringLength:self.maxTextLength];
|
|||
|
|
// 移动光标
|
|||
|
|
textView.selectedRange = NSMakeRange(textView.text.length, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (self.didChangeText) {
|
|||
|
|
self.didChangeText(self);
|
|||
|
|
}
|
|||
|
|
if ([self.delegate respondsToSelector:@selector(didChangeText:)]) {
|
|||
|
|
[self.delegate didChangeText:self];
|
|||
|
|
}
|
|||
|
|
_placeHolderLabel.hidden = textView.text.length;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)clear {
|
|||
|
|
self.textview.text = nil;
|
|||
|
|
_placeHolderLabel.hidden = NO;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#pragma mark tool--
|
|||
|
|
- (void)resignFirstResp
|
|||
|
|
{
|
|||
|
|
if([_textview canBecomeFirstResponder])
|
|||
|
|
{
|
|||
|
|
[_textview becomeFirstResponder];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#pragma mark - Getter
|
|||
|
|
|
|||
|
|
- (NSString *)content
|
|||
|
|
{
|
|||
|
|
return _textview.text;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@end
|