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

Commit 1df5c90

Browse files
committed
修复了直接使用 init 创建时,占位符、TextView 大小的显示问题。
1 parent 2980b82 commit 1df5c90

File tree

6 files changed

+93
-95
lines changed

6 files changed

+93
-95
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
### RKOTextView
4848

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

@@ -58,7 +58,7 @@
5858
#### 集成:
5959

6060
```shell
61-
pod 'RKOTextView', '~> 1.1.0'
61+
pod 'RKOTextView', '~> 1.1.1'
6262
```
6363

6464
#### 基本使用:

RKOTextViewManager/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# RKOTextView
22

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

@@ -39,7 +39,7 @@
3939
## 集成
4040

4141
```shell
42-
pod 'RKOTextView', '~> 1.1.0'
42+
pod 'RKOTextView', '~> 1.1.1'
4343
```
4444

4545
## 基本使用

RKOTextViewManager/RKOTextView.podspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Pod::Spec.new do |s|
1010

1111
s.name = "RKOTextView"
12-
s.version = "1.1.0"
12+
s.version = "1.1.1"
1313
s.summary = "A UITextView package"
1414
s.description = <<-DESC
1515
A UITextView package
@@ -21,9 +21,9 @@ Pod::Spec.new do |s|
2121

2222
s.author = { "Rakuyo" => "[email protected]" }
2323

24-
s.ios.deployment_target = "7.0"
24+
s.ios.deployment_target = "9.0"
2525

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

2828
s.requires_arc = true
2929

RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.h

+22-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@class RKOTextView;
1414

15-
#pragma mark - 代理。
15+
#pragma mark - delegate
1616
@protocol RKOTextViewDelegate <NSObject, UITextViewDelegate>
1717
@optional
1818

@@ -26,7 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
2626

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

4343
@interface RKOTextView : UITextView
4444

45-
#pragma mark - 方法。
45+
#pragma mark - method
4646

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

65-
#pragma mark - 属性。
66+
#pragma mark - property
6667

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

70-
/** 占位符文字。 */
71-
@property (nonatomic, copy, nullable) IBInspectable NSString * placeholder;
74+
/**
75+
* 占位符文字。
76+
* 请注意,先设置 TextView 的 text,再设置 placeholder
77+
*/
78+
@property (nonatomic, copy, nullable) IBInspectable NSString *placeholder;
7279

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

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

79-
/** TextView 显示的最大行数 */
89+
/** TextView 显示的最大行数 */
8090
@property (nonatomic, assign) IBInspectable NSUInteger maxRows;
8191

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

RKOTextViewManager/RKOTextView/RKOTextView/RKOTextView/RKOTextView.m

+61-72
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@ + (RKOTextView *)textViewWithFrame:(CGRect)frame
4444
textView.maxCharacters = maxCharacters;
4545
textView.maxRows = maxRows;
4646

47-
// 设置样式。
48-
[textView configTextViewStyle];
49-
5047
return textView;
5148
}
5249

50+
- (instancetype)init {
51+
if (self = [super init]) {
52+
[self configDefineSytle];
53+
}
54+
return self;
55+
}
56+
5357
// 当从storyboard/xib中初始化该控件的时候
5458
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
55-
5659
if (!(self = [super initWithCoder:aDecoder])) return nil;
5760

58-
// 设置样式
59-
[self configTextViewStyle];
61+
[self configDefineSytle];
6062

6163
return self;
6264
}
6365

64-
- (void)configTextViewStyle {
66+
- (void)configDefineSytle {
6567

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

88-
// 初始属性。
89-
self.needBorder = NO;
90+
// 记录上次输入内容
91+
self.lastTimeInput = self.hasText ? self.text : @"";
9092

91-
if (self.hasText) {
92-
self.lastTimeInput = self.text;
93-
} else {
94-
self.lastTimeInput = [NSString string];
95-
}
96-
97-
// 设置代理,监听文字输入
9893
self.delegate = self;
9994
}
10095

@@ -140,8 +135,8 @@ - (void)textViewDidChange:(UITextView *)textView {
140135
}
141136

142137
// 提供代理,供用户监听输入
143-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidChange:)]) {
144-
[self.textViewDelegate textViewDidChange:textView];
138+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidChange:)]) {
139+
[self.rko_textViewDelegate textViewDidChange:textView];
145140
}
146141
}
147142

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

156-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
157-
[self.textViewDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
151+
if (self.delegate && [self.delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)]) {
152+
[self.delegate textView:textView shouldChangeTextInRange:range replacementText:text];
158153
}
159154

160155
return YES;
@@ -297,8 +292,8 @@ typedef NS_ENUM(NSUInteger, TriggerType) {
297292
}
298293

299294
// 显示提示窗
300-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxRows:)]) {
301-
[self.textViewDelegate textViewDidAchieveMaxRows:textView];
295+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxRows:)]) {
296+
[self.rko_textViewDelegate textViewDidAchieveMaxRows:textView];
302297
}
303298
}
304299

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

328323
// 显示提示窗,提示字数限制
329-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
330-
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
324+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
325+
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
331326
}
332327
}
333328
}
@@ -351,9 +346,10 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
351346
return YES;
352347
} else {
353348
// 显示提示窗,提示字数限制
354-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
355-
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
349+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
350+
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
356351
}
352+
357353
return NO;
358354
}
359355
}
@@ -392,8 +388,8 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
392388
// 取出所需要就break,提高效率
393389
*stop = YES;
394390
// 显示提示窗,提示字数限制
395-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
396-
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
391+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
392+
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
397393
}
398394
return ;
399395
}
@@ -414,32 +410,15 @@ - (BOOL)limitInputWithTextView:(UITextView *)textView range:(NSRange)range repla
414410
// 判断子视图是否显示,以及适配高度
415411
[self judgmentSubviewsDisplayed:textView];
416412
// 显示提示窗,提示字数限制
417-
if (self.textViewDelegate && [self.textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
418-
[self.textViewDelegate textViewDidAchieveMaxCharacters:textView];
413+
if (self.rko_textViewDelegate && [self.rko_textViewDelegate respondsToSelector:@selector(textViewDidAchieveMaxCharacters:)]) {
414+
[self.rko_textViewDelegate textViewDidAchieveMaxCharacters:textView];
419415
}
416+
420417
return NO;
421418
}
422419
}
423420

424421
#pragma mark - 占位符
425-
// 初始化占位符Lable
426-
- (void)createPlaceholderLabel {
427-
428-
// 创建并设置占位符Label
429-
self.placeholderLabel = [[UILabel alloc] init];
430-
431-
self.placeholderLabel.backgroundColor = [UIColor clearColor];
432-
self.placeholderLabel.numberOfLines = 0;
433-
//设置占位文字默认颜色
434-
self.placeholderLabel.textColor = [UIColor lightGrayColor];
435-
436-
// 添加视图
437-
[self addSubview:self.placeholderLabel];
438-
439-
// 根据初始状态判断占位符是否显示
440-
self.placeholderLabel.hidden = self.hasText;
441-
}
442-
443422
// 对占位符Label进行布局
444423
- (void)layoutPlaceholderLabel {
445424

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

457-
// 重写字体的设置方法,保证两个字的大小一样
458-
- (void)setFont:(UIFont *)font {
459-
[super setFont:font];
460-
461-
if (!self.placeholder || self.placeholder.length == 0) {
462-
return;
463-
}
464-
465-
// 如果传nil的话则为系统默认大小。
466-
if (!font) {
467-
font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
468-
}
436+
// 重写set方法,设置占位符文字
437+
- (void)setPlaceholder:(NSString *)placeholder {
438+
_placeholder = [placeholder copy];
469439

470-
// 修改占位符文字的大小。
471-
self.placeholderLabel.font = font;
440+
//设置文字
441+
self.placeholderLabel.text = placeholder;
472442

473443
//重新计算子控件frame
474444
[self setNeedsLayout];
475445
}
476446

477-
// 重写set方法,设置占位符文字
478-
- (void)setPlaceholder:(NSString *)placeholder {
479-
480-
_placeholder = [placeholder copy];
447+
- (UILabel *)placeholderLabel {
448+
if (!_placeholderLabel) {
449+
_placeholderLabel = [[UILabel alloc] init];
450+
451+
_placeholderLabel.backgroundColor = [UIColor clearColor];
452+
_placeholderLabel.numberOfLines = 0;
453+
454+
_placeholderLabel.textColor = [UIColor lightGrayColor];
455+
456+
[self addSubview:_placeholderLabel];
457+
458+
_placeholderLabel.hidden = self.hasText;
459+
}
460+
return _placeholderLabel;
461+
}
462+
463+
// 保证 font 有值
464+
- (UIFont *)font {
465+
if ([super font]) return [super font];
466+
return [UIFont systemFontOfSize:[UIFont systemFontSize]];
467+
}
468+
469+
// 保持占位符与 TextView 字体相同
470+
- (void)setFont:(UIFont *)font {
471+
[super setFont:font];
481472

482-
// 初始化占位符Lable
483-
[self createPlaceholderLabel];
473+
NSString *placeholder = [self.placeholder stringByReplacingOccurrencesOfString:@" " withString:@""];
484474

485-
//设置文字
486-
_placeholderLabel.text = placeholder;
475+
if (![placeholder isEqualToString:@""] && placeholder.length != 0) {
476+
self.placeholderLabel.font = font;
477+
}
487478

488479
//重新计算子控件frame
489480
[self setNeedsLayout];
490481
}
491482

492483
@end
493-
494-

RKOTextViewManager/RKOTextView/RKOTextView/ViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ - (void)setUpWithStroyBoard {
3737
*/
3838

3939
// 设置代理
40-
self.testTextView.textViewDelegate = self;
40+
self.testTextView.rko_textViewDelegate = self;
4141

4242
// 显示默认边框
4343
self.testTextView.needBorder = YES;

0 commit comments

Comments
 (0)