Skip to content

Commit

Permalink
UITextView Placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Oranges and lemons authored and Oranges and lemons committed Jan 25, 2018
1 parent ac515c8 commit 87c4bd0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion IBProperty.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "IBProperty"
s.version = "1.1.0"
s.version = "1.1.1"
s.ios.deployment_target = '7.0'
s.summary = "A iOS Kit which would adapt screen by XIB and Storyboard better, and use better for XIB and Storyboard。"
#s.description = <<-DESC
Expand Down
4 changes: 3 additions & 1 deletion IBProperty/NSLayoutConstraint+ORIBProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface NSLayoutConstraint (ORIBProperty)

/*
Expand All @@ -17,7 +19,7 @@
@property (nonatomic, assign) IBInspectable BOOL ib_adaptConstant;

/*
* 适配导航栏高度,若为YES constant将不会适配比例,而是在iPhone X 上加上24pt, 常用于 为自定义导航栏的高度约束以及子视图布局添加约束
* 适配导航栏高度,若为YES constant将不会适配比例,而是在iPhone X 上加上24pt, 常用于 为自定义导航栏的子视图添加约束
* constant would not adapt but add 24px in iPhone X
*/
@property (nonatomic, assign) IBInspectable BOOL ib_adaptXTopConstant;
Expand Down
2 changes: 2 additions & 0 deletions IBProperty/UICollectionViewFlowLayout+ORIBProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface UICollectionViewFlowLayout (ORIBProperty)

/* itemSize、minimumLineSpacing、minimumInteritemSpacing、headerReferenceSize、footerReferenceSize、sectionInset
Expand Down
2 changes: 2 additions & 0 deletions IBProperty/UIControl+ORIBProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface UIControl (ORIBProperty)

/*
Expand Down
8 changes: 8 additions & 0 deletions IBProperty/UITextView+ORIBProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface UITextView (ORIBProperty)

/*
Expand All @@ -16,5 +18,11 @@
*/
@property (nonatomic, assign) IBInspectable BOOL ib_adaptFont;

/*
* 类似于UITextField的placeholder
* like UITextField's placeholder
*/
@property (nonatomic, copy) IBInspectable NSString * ib_placeholder;


@end
69 changes: 69 additions & 0 deletions IBProperty/UITextView+ORIBProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
#import "UITextView+ORIBProperty.h"
#import "ORIBProperty.h"

static NSInteger const placeholderTag = 2017;

@implementation UITextView (ORIBProperty)

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setIb_adaptFont:(BOOL)ib_adaptFont {

if (ib_adaptFont == YES) {
Expand All @@ -22,6 +28,69 @@ - (BOOL)ib_adaptFont {
return NO;
}

- (void)setIb_placeholder:(NSString *)ib_placeholder {
if (ib_placeholder.length > 0) {
UILabel *label = [self viewWithTag:placeholderTag];
if (!label) {
label = [UILabel new];
label.font = self.font;
label.textColor = [UIColor colorWithRed:213/255.f green:213/255.f blue:213/255.f alpha:1];
label.tag = placeholderTag;
label.frame = CGRectMake(self.textContainerInset.left + 5, self.textContainerInset.top, self.frame.size.width - (self.textContainerInset.left + self.textContainerInset.right), self.frame.size.height - (self.textContainerInset.top + self.textContainerInset.bottom));
label.numberOfLines = 0;
[self addSubview:label];
}
label.text = ib_placeholder;
[label sizeToFit];
label.hidden = NO;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textBeginEditing) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textEndEditing) name:UITextViewTextDidEndEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];

__weak typeof (self) weakSelf = self;

[self aspect_hookSelector:@selector(setText:) withOptions:AspectPositionAfter usingBlock:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.ib_plceholderLabel.hidden = strongSelf.text.length > 0 || [strongSelf isFirstResponder];
} error:nil];

[self aspect_hookSelector:@selector(setTextContainerInset:) withOptions:AspectPositionAfter usingBlock:^{

__strong typeof(weakSelf) strongSelf = weakSelf;

CGRect frame = strongSelf.ib_plceholderLabel.frame;
frame.origin.x = strongSelf.textContainerInset.left + 5;
frame.origin.y = strongSelf.textContainerInset.top;
strongSelf.ib_plceholderLabel.frame = frame;
} error:nil];

[self aspect_hookSelector:@selector(setFont:) withOptions:AspectPositionAfter usingBlock:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.ib_plceholderLabel.font = strongSelf.font;
} error:nil];
}
}

- (NSString *)ib_placeholder {
return self.ib_plceholderLabel.text;
}

- (void)textBeginEditing {
self.ib_plceholderLabel.hidden = YES;
}

- (void)textEndEditing {
self.ib_plceholderLabel.hidden = self.text.length > 0;
}

- (void)textDidChange {
self.ib_plceholderLabel.hidden = self.text.length > 0 || [self isFirstResponder];
}

- (UILabel *)ib_plceholderLabel {
return [self viewWithTag:placeholderTag];
}

@end

8 changes: 4 additions & 4 deletions IBProperty/UIView+ORIBProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ - (UIImage*)imageAddib_cornerRadius:(CGFloat)radius andSize:(CGSize)size;
@implementation UIImage(ORIBProperty)

- (UIImage*)imageAddib_cornerRadius:(CGFloat)radius andSize:(CGSize)size{
CGRect rect = CGRectMake(0, 0, size.width, size.height);

CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
Expand Down Expand Up @@ -59,7 +59,7 @@ - (void)setIb_borderColor:(UIColor *)ib_borderColor {
}

- (UIColor *)ib_borderColor {
return nil;
return [UIColor colorWithCGColor:self.layer.borderColor];
}

#pragma mark -- ib_cornerRadius
Expand Down Expand Up @@ -113,8 +113,6 @@ - (void)setIb_cornerCircle:(BOOL)ib_cornerCircle {

self.layer.cornerRadius = self.bounds.size.height / 2.0f;

__weak typeof (self) weakSelf = self;

if ([self isKindOfClass:[UIImageView class]]) {
[self method_exchangeWithSelector:@selector(setImage:) toSelector:@selector(ib_setImage:)];
}
Expand All @@ -130,6 +128,8 @@ - (void)setIb_cornerCircle:(BOOL)ib_cornerCircle {
}
}

__weak typeof (self) weakSelf = self;

[self aspect_hookSelector:@selector(setBounds:) withOptions:AspectPositionAfter usingBlock:^(){

__strong typeof(weakSelf) strongSelf = weakSelf;
Expand Down

0 comments on commit 87c4bd0

Please sign in to comment.