Skip to content

Commit

Permalink
添加最小高度属性,可以设置textView最小高度
Browse files Browse the repository at this point in the history
  • Loading branch information
王振标 committed Apr 16, 2017
1 parent cb90112 commit c93f571
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions WZBTextView-demo/WZBTextView/UITextView+WZB.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef void(^textViewHeightDidChangedBlock)(CGFloat currentTextViewHeight);
@property (nonatomic, strong) UIColor *placeholderColor;
/* 最大高度,如果需要随文字改变高度的时候使用 */
@property (nonatomic, assign) CGFloat maxHeight;
/* 最小高度,如果需要随文字改变高度的时候使用 */
@property (nonatomic, assign) CGFloat minHeight;
@property (nonatomic, copy) textViewHeightDidChangedBlock textViewHeightDidChanged;
/* 获取图片数组 */
- (NSArray *)getImages;
Expand Down
30 changes: 20 additions & 10 deletions WZBTextView-demo/WZBTextView/UITextView+WZB.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
static const void *WZBPlaceholderColorKey = &WZBPlaceholderColorKey;
// 最大高度
static const void *WZBTextViewMaxHeightKey = &WZBTextViewMaxHeightKey;
// 最小高度
static const void *WZBTextViewMinHeightKey = &WZBTextViewMinHeightKey;
// 高度变化的block
static const void *WZBTextViewHeightDidChangedBlockKey = &WZBTextViewHeightDidChangedBlockKey;
// 存储添加的图片
Expand Down Expand Up @@ -145,6 +147,14 @@ - (CGFloat)maxHeight {
return [objc_getAssociatedObject(self, WZBTextViewMaxHeightKey) doubleValue];
}

- (void)setMinHeight:(CGFloat)minHeight {
objc_setAssociatedObject(self, WZBTextViewMinHeightKey, [NSString stringWithFormat:@"%lf", minHeight], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGFloat)minHeight {
return [objc_getAssociatedObject(self, WZBTextViewMinHeightKey) doubleValue];
}

- (void)setLastHeight:(CGFloat)lastHeight {
objc_setAssociatedObject(self, WZBTextViewLastHeightKey, [NSString stringWithFormat:@"%lf", lastHeight], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
Expand Down Expand Up @@ -212,13 +222,15 @@ - (void)textViewTextChange {
self.scrollEnabled = currentHeight >= self.maxHeight;
CGFloat currentTextViewHeight = currentHeight >= self.maxHeight ? self.maxHeight : currentHeight;
// 改变textView的高度
CGRect frame = self.frame;
frame.size.height = currentTextViewHeight;
self.frame = frame;
// 调用block
if (self.textViewHeightDidChanged) self.textViewHeightDidChanged(currentTextViewHeight);
// 记录当前高度
self.lastHeight = currentTextViewHeight;
if (currentTextViewHeight >= self.minHeight) {
CGRect frame = self.frame;
frame.size.height = currentTextViewHeight;
self.frame = frame;
// 调用block
if (self.textViewHeightDidChanged) self.textViewHeightDidChanged(currentTextViewHeight);
// 记录当前高度
self.lastHeight = currentTextViewHeight;
}
}
}

Expand All @@ -241,9 +253,7 @@ - (BOOL)placeholderExist {
UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

// 如果有placeholder值
if (placeholderView) {
return YES;
}
if (placeholderView) return YES;

return NO;
}
Expand Down

0 comments on commit c93f571

Please sign in to comment.