Skip to content

Commit

Permalink
解决当设置最大高度小于textView本身高度的时候不能滚动的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
WZBbiao committed Dec 28, 2016
1 parent d0d599f commit 3573ffc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
3 changes: 3 additions & 0 deletions WZBTextView-demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
986FE9C71DD9AB190091617F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
986FE9C81DD9AB190091617F /* Build configuration list for PBXNativeTarget "WZBTextView-demoTests" */ = {
isa = XCConfigurationList;
Expand All @@ -536,6 +537,7 @@
986FE9CA1DD9AB190091617F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
986FE9CB1DD9AB190091617F /* Build configuration list for PBXNativeTarget "WZBTextView-demoUITests" */ = {
isa = XCConfigurationList;
Expand All @@ -544,6 +546,7 @@
986FE9CD1DD9AB190091617F /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
14 changes: 13 additions & 1 deletion WZBTextView-demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) UITextView *tView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self test1];
}

- (void)test1 {
self.textView.hidden = NO;
self.textView.placeholder = @"i love you";
self.textView.maxHeight = 100.05;
}

- (void)test2 {
self.textView.hidden = YES;

UITextView *textView = [[UITextView alloc] initWithFrame:(CGRect){0, 0, 200, 30}];
[self.view addSubview:textView];
textView.center = self.view.center;
Expand All @@ -35,7 +47,7 @@ - (void)viewDidLoad {
}];
}];
self.tView = textView;
UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){20, 30, 50, 25}];
UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){20, 30, 100, 25}];
[self.view addSubview:button];
[button setTitle:@"添加图片" forState:UIControlStateNormal];
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
Expand Down
48 changes: 37 additions & 11 deletions WZBTextView-demo/WZBTextView/UITextView+WZB.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,36 @@ @interface UITextView ()
@implementation UITextView (WZB)

- (UITextView *)placeholderView {

// 为了让占位文字和textView的实际文字位置能够完全一致,这里用UITextView
UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

if (!placeholderView) {

placeholderView = [[UITextView alloc] init];

// 动态添加属性的本质是: 让对象的某个属性与值产生关联
objc_setAssociatedObject(self, WZBPlaceholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
placeholderView = placeholderView;

// 设置基本属性
self.scrollEnabled = placeholderView.scrollEnabled = placeholderView.showsHorizontalScrollIndicator = placeholderView.showsVerticalScrollIndicator = placeholderView.userInteractionEnabled = NO;
placeholderView.textColor = [UIColor lightGrayColor];
placeholderView.backgroundColor = [UIColor clearColor];
[self refreshPlaceholderView];
[self addSubview:placeholderView];

// 监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextChange) name:UITextViewTextDidChangeNotification object:self];

// 这些属性改变时,都要作出一定的改变,尽管已经监听了TextDidChange的通知,也要监听text属性,因为通知监听不到setText:
NSArray *propertys = @[@"frame", @"bounds", @"font", @"text", @"textAlignment", @"textContainerInset"];

// 监听属性
for (NSString *property in propertys) {
[self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil];
}

}
return placeholderView;
}
Expand Down Expand Up @@ -80,28 +89,23 @@ - (void)setPlaceholder:(NSString *)placeholder

- (NSString *)placeholder
{
// 获取对应属性的值

UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

// 如果有placeholder值才去调用,这步很重要
if (placeholderView) {
if (self.placeholderExist) {
return [self placeholderView].text;
}
return nil;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
// 动态添加属性的本质是: 让对象的某个属性与值产生关联
objc_setAssociatedObject(self, WZBPlaceholderColorKey, placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

// 如果有placeholder值才去调用,这步很重要
if (placeholderView) {
if (self.placeholderExist) {
NSLog(@"请先设置placeholder值!");
} else {
self.placeholderView.textColor = placeholderColor;

// 动态添加属性的本质是: 让对象的某个属性与值产生关联
objc_setAssociatedObject(self, WZBPlaceholderColorKey, placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}

Expand All @@ -110,7 +114,15 @@ - (UIColor *)placeholderColor {
}

- (void)setMaxHeight:(CGFloat)maxHeight {
objc_setAssociatedObject(self, WZBTextViewMaxHeightKey, [NSString stringWithFormat:@"%lf", maxHeight], OBJC_ASSOCIATION_COPY_NONATOMIC);

CGFloat max = maxHeight;

// 如果传入的最大高度小于textView本身的高度,则让最大高度等于本身高度
if (maxHeight < self.frame.size.height) {
max = self.frame.size.height;
}

objc_setAssociatedObject(self, WZBTextViewMaxHeightKey, [NSString stringWithFormat:@"%lf", max], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGFloat)maxHeight {
Expand Down Expand Up @@ -160,6 +172,20 @@ - (void)autoHeightWithMaxHeight:(CGFloat)maxHeight textViewHeightDidChanged:(voi
self.textViewHeightDidChanged = textViewHeightDidChanged;
}

// 判断是否有placeholder值,这步很重要
- (BOOL)placeholderExist {

// 获取对应属性的值
UITextView *placeholderView = objc_getAssociatedObject(self, WZBPlaceholderViewKey);

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

return NO;
}

- (void)addImage:(UIImage *)image {
[self addImage:image size:CGSizeZero];
}
Expand Down

0 comments on commit 3573ffc

Please sign in to comment.