Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
修复了直接使用 init 创建时,占位符、TextView 大小的显示问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuyoMo committed Mar 7, 2018
1 parent 2980b82 commit 1df5c90
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 95 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
### RKOTextView

<p align="left">
<a href=""><img src="https://img.shields.io/badge/pod-v1.1.0-brightgreen.svg"></a>
<a href=""><img src="https://img.shields.io/badge/pod-v1.1.1-brightgreen.svg"></a>
<a href=""><img src="https://img.shields.io/badge/ObjectiveC-compatible-orange.svg"></a>
<a href=""><img src="https://img.shields.io/badge/platform-iOS%207.0%2B-ff69b5152950834.svg"></a>
<a href=""><img src="https://img.shields.io/badge/platform-iOS%209.0%2B-ff69b5152950834.svg"></a>
<a href="https://github.com/rakuyoMo/RKOTools/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green.svg?style=flat"></a>
</p>

Expand All @@ -58,7 +58,7 @@
#### 集成:

```shell
pod 'RKOTextView', '~> 1.1.0'
pod 'RKOTextView', '~> 1.1.1'
```

#### 基本使用:
Expand Down
6 changes: 3 additions & 3 deletions RKOTextViewManager/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# RKOTextView

<p align="center">
<a href=""><img src="https://img.shields.io/badge/pod-v1.1.0-brightgreen.svg"></a>
<a href=""><img src="https://img.shields.io/badge/pod-v1.1.1-brightgreen.svg"></a>
<a href=""><img src="https://img.shields.io/badge/ObjectiveC-compatible-orange.svg"></a>
<a href=""><img src="https://img.shields.io/badge/platform-iOS%207.0%2B-ff69b5152950834.svg"></a>
<a href=""><img src="https://img.shields.io/badge/platform-iOS%209.0%2B-ff69b5152950834.svg"></a>
<a href="https://github.com/rakuyoMo/RKOTools/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green.svg?style=flat"></a>
</p>

Expand Down Expand Up @@ -39,7 +39,7 @@
## 集成

```shell
pod 'RKOTextView', '~> 1.1.0'
pod 'RKOTextView', '~> 1.1.1'
```

## 基本使用
Expand Down
6 changes: 3 additions & 3 deletions RKOTextViewManager/RKOTextView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = "RKOTextView"
s.version = "1.1.0"
s.version = "1.1.1"
s.summary = "A UITextView package"
s.description = <<-DESC
A UITextView package
Expand All @@ -21,9 +21,9 @@ Pod::Spec.new do |s|

s.author = { "Rakuyo" => "rakuyo.mo@gmail.com" }

s.ios.deployment_target = "7.0"
s.ios.deployment_target = "9.0"

s.source = { :git => "https://github.com/rakuyoMo/RKOControls.git", :tag => "1.0.8" } # #{s.version}
s.source = { :git => "https://github.com/rakuyoMo/RKOControls.git", :tag => "1.3.12" } # #{s.version}

s.requires_arc = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN

@class RKOTextView;

#pragma mark - 代理。
#pragma mark - delegate
@protocol RKOTextViewDelegate <NSObject, UITextViewDelegate>
@optional

Expand All @@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN

/**
* 自定义 UITextView ,适配纯代码及 xib、storyboard。
*
* 本控件额外提供以下功能:
*
* 1. 兼容 stroyboard、xib 以及纯代码。
Expand All @@ -42,10 +42,11 @@ NS_ASSUME_NONNULL_BEGIN

@interface RKOTextView : UITextView

#pragma mark - 方法。
#pragma mark - method

/**
* 快速创建对象并设置其样式。(若您使用纯代码方式,推荐使用该方法。)
* 快速创建对象并设置其样式。
*(若您使用纯代码且不使用自动布局,那么推荐使用该方法。)
*
* @param frame 视图大小及位置(若您需要自适应视图的高度,请在高度处填 0 )
* @param placeholder 占位符文字
Expand All @@ -62,24 +63,32 @@ NS_ASSUME_NONNULL_BEGIN
maxCharacters:(NSUInteger)maxCharacters
maxRows:(NSUInteger)maxRows;

#pragma mark - 属性。
#pragma mark - property

/** 代理 */
@property (nonatomic, weak) id<RKOTextViewDelegate> textViewDelegate;
/**
* 代理
* 请使用该代理来代替 TextView 原先的 delegate,不要再对 delegate 进行修改/赋值。
*/
@property (nonatomic, weak) id<RKOTextViewDelegate> rko_textViewDelegate;

/** 占位符文字。 */
@property (nonatomic, copy, nullable) IBInspectable NSString * placeholder;
/**
* 占位符文字。
* 请注意,先设置 TextView 的 text,再设置 placeholder
*/
@property (nonatomic, copy, nullable) IBInspectable NSString *placeholder;

/** 开启该属性后,在达到最大行数时将无法进行输入。(优先级于最大字符数,但要同时设置最大行数) */
/**
* 开启该属性后,在达到最大行数时将无法进行输入。
* (优先级于最大字符数,但要同时设置最大行数)
*/
@property (nonatomic, assign) IBInspectable BOOL limitInputRange;

/** 可输入的最大字符数(优先于最大行数) */
/** 可输入的最大字符数(优先于最大行数) */
@property (nonatomic, assign) IBInspectable NSUInteger maxCharacters;

/** TextView 显示的最大行数 */
/** TextView 显示的最大行数 */
@property (nonatomic, assign) IBInspectable NSUInteger maxRows;


/** 是否显示本控件提供的边框 */
@property (nonatomic, assign) IBInspectable BOOL needBorder;

Expand Down
133 changes: 61 additions & 72 deletions RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,26 @@ + (RKOTextView *)textViewWithFrame:(CGRect)frame
textView.maxCharacters = maxCharacters;
textView.maxRows = maxRows;

// 设置样式。
[textView configTextViewStyle];

return textView;
}

- (instancetype)init {
if (self = [super init]) {
[self configDefineSytle];
}
return self;
}

// 当从storyboard/xib中初始化该控件的时候
- (instancetype)initWithCoder:(NSCoder *)aDecoder {

if (!(self = [super initWithCoder:aDecoder])) return nil;

// 设置样式
[self configTextViewStyle];
[self configDefineSytle];

return self;
}

- (void)configTextViewStyle {
- (void)configDefineSytle {

// 设置行间距以及换行模式。
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
Expand All @@ -85,16 +87,9 @@ - (void)configTextViewStyle {
self.showsHorizontalScrollIndicator = NO;
self.enablesReturnKeyAutomatically = YES;

// 初始属性。
self.needBorder = NO;
// 记录上次输入内容
self.lastTimeInput = self.hasText ? self.text : @"";

if (self.hasText) {
self.lastTimeInput = self.text;
} else {
self.lastTimeInput = [NSString string];
}

// 设置代理,监听文字输入
self.delegate = self;
}

Expand Down Expand Up @@ -140,8 +135,8 @@ - (void)textViewDidChange:(UITextView *)textView {
}

// 提供代理,供用户监听输入
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidChange:)]) {
[self.textViewDelegate textViewDidChange:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidChange:)]) {
[self.rko_textViewDelegate textViewDidChange:textView];
}
}

Expand All @@ -153,8 +148,8 @@ - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
return [self limitInputWithTextView:textView range:range replacementText:text];
}

if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
[self.textViewDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
if (self.delegate && [self.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
[self.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
}

return YES;
Expand Down Expand Up @@ -297,8 +292,8 @@ typedef NS_ENUM(NSUInteger, TriggerType) {
}

// 显示提示窗
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxRows:)]) {
[self.textViewDelegate textViewDidAchieveMaxRows:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxRows:)]) {
[self.rko_textViewDelegate textViewDidAchieveMaxRows:textView];
}
}

Expand Down Expand Up @@ -326,8 +321,8 @@ - (void)calculateNumberOfRemainingCharacters:(UITextView *)textView {
textView.text = str;

// 显示提示窗,提示字数限制
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
}
}
}
Expand All @@ -351,9 +346,10 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
return YES;
} else {
// 显示提示窗,提示字数限制
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
}

return NO;
}
}
Expand Down Expand Up @@ -392,8 +388,8 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
// 取出所需要就break,提高效率
*stop = YES;
// 显示提示窗,提示字数限制
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
}
return ;
}
Expand All @@ -414,32 +410,15 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
// 判断子视图是否显示,以及适配高度
[self judgmentSubviewsDisplayed:textView];
// 显示提示窗,提示字数限制
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
}

return NO;
}
}

#pragma mark - 占位符
// 初始化占位符Lable
- (void)createPlaceholderLabel {

// 创建并设置占位符Label
self.placeholderLabel = [[UILabel alloc] init];

self.placeholderLabel.backgroundColor = [UIColor clearColor];
self.placeholderLabel.numberOfLines = 0;
//设置占位文字默认颜色
self.placeholderLabel.textColor = [UIColor lightGrayColor];

// 添加视图
[self addSubview:self.placeholderLabel];

// 根据初始状态判断占位符是否显示
self.placeholderLabel.hidden = self.hasText;
}

// 对占位符Label进行布局
- (void)layoutPlaceholderLabel {

Expand All @@ -454,41 +433,51 @@ - (void)layoutPlaceholderLabel {
self.placeholderLabel.frame = frame;
}

// 重写字体的设置方法,保证两个字的大小一样
- (void)setFont:(UIFont *)font {
[super setFont:font];

if (!self.placeholder || self.placeholder.length == 0) {
return;
}

// 如果传nil的话则为系统默认大小。
if (!font) {
font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
}
// 重写set方法,设置占位符文字
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = [placeholder copy];

// 修改占位符文字的大小。
self.placeholderLabel.font = font;
//设置文字
self.placeholderLabel.text = placeholder;

//重新计算子控件frame
[self setNeedsLayout];
}

// 重写set方法,设置占位符文字
- (void)setPlaceholder:(NSString *)placeholder {

_placeholder = [placeholder copy];
- (UILabel *)placeholderLabel {
if (!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] init];

_placeholderLabel.backgroundColor = [UIColor clearColor];
_placeholderLabel.numberOfLines = 0;

_placeholderLabel.textColor = [UIColor lightGrayColor];

[self addSubview:_placeholderLabel];

_placeholderLabel.hidden = self.hasText;
}
return _placeholderLabel;
}

// 保证 font 有值
- (UIFont *)font {
if ([super font]) return [super font];
return [UIFont systemFontOfSize:[UIFont systemFontSize]];
}

// 保持占位符与 TextView 字体相同
- (void)setFont:(UIFont *)font {
[super setFont:font];

// 初始化占位符Lable
[self createPlaceholderLabel];
NSString *placeholder = [self.placeholder stringByReplacingOccurrencesOfString:@" " withString:@""];

//设置文字
_placeholderLabel.text = placeholder;
if (![placeholder isEqualToString:@""] && placeholder.length != 0) {
self.placeholderLabel.font = font;
}

//重新计算子控件frame
[self setNeedsLayout];
}

@end


Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (void)setUpWithStroyBoard {
*/

// 设置代理
self.testTextView.textViewDelegate = self;
self.testTextView.rko_textViewDelegate = self;

// 显示默认边框
self.testTextView.needBorder = YES;
Expand Down

0 comments on commit 1df5c90

Please sign in to comment.