iOS给UITextView添加Placeholder
2017-03-06 12:09
344 查看
// // LAXTextView.h // LAXTextViewPlaceHolder // // Created by 刘翱翔 on 2017/1/7. // Copyright © 2017年 liuaoxiang. All rights reserved. // #import <UIKit/UIKit.h> @interface LAXTextView : UITextView @property(nonatomic, assign) BOOL hasText2; @property(nonatomic,copy) NSString *placeholder; //文字 @property(nonatomic,strong) UIColor *placeholderColor; //文字颜色 @end
// // LAXTextView.m // LAXTextViewplaceholder // // Created by 刘翱翔 on 2017/1/7. // Copyright © 2017年 liuaoxiang. All rights reserved. // #import "LAXTextView.h" #import "NSObject+KVOCategory.h" @interface LAXTextView () @property(nonatomic,copy) UIFont *placeholderFont; @property (nonatomic, strong) UILabel * placeholderLabel; @end @implementation LAXTextView #pragma mark - Setters - (UILabel *)placeholderLabel { if (!_placeholderLabel) { [self setup]; } return _placeholderLabel; } - (void)setPlaceholder:(NSString *)placeholder { if([placeholder isEqualToString:_placeholder]) { return; } _placeholder = placeholder; self.placeholderLabel.text = placeholder; [self setNeedsDisplay]; [self notificationAction]; } - (void)setPlaceholderColor:(UIColor *)placeholderColor { if([placeholderColor isEqual:_placeholderColor]) { return; } _placeholderColor = placeholderColor; self.placeholderLabel.textColor = _placeholderColor; [self setNeedsDisplay]; [self notificationAction]; } #pragma mark - Message text view - (NSUInteger)numberOfLinesOfText { return [LAXTextView numberOfLinesForMessage:self.text]; } + (NSUInteger)maxCharactersPerLine { return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? 33 : 109; } + (NSUInteger)numberOfLinesForMessage:(NSString *)text { return (text.length / [LAXTextView maxCharactersPerLine]) + 1; } #pragma mark - Text view overrides - (void)setText:(NSString *)text { self.hasText2 = NO; if (text.length > 0) { self.hasText2 = YES; [self notificationAction]; } [super setText:text]; [self setNeedsDisplay]; } - (void)setAttributedText:(NSAttributedString *)attributedText { [super setAttributedText:attributedText]; [self setNeedsDisplay]; } - (void)setFont:(UIFont *)font { [self setPlaceholderFrame]; self.placeholderFont = font; self.placeholderLabel.font = font; [super setFont:font]; [self setNeedsDisplay]; } - (void)setTextAlignment:(NSTextAlignment)textAlignment { [super setTextAlignment:textAlignment]; [self setNeedsDisplay]; } - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (void)dealloc { _placeholder = nil; _placeholderColor = nil; [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification]; // [self.KVOController unobserveAll]; //end_code } #pragma mark - custom func - (CGRect)getRectFromString:(NSString *)string fontSize:(UIFont *)font { NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]}; CGRect rect = [string boundingRectWithSize:CGSizeMake(100, 1000) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil]; return rect; } - (void)setPlaceholderFrame { // self.backgroundColor = [UIColor blueColor]; if (self.placeholderLabel != nil) { // CGFloat h = [self getRectFromString:_placeholder fontSize:_placeholderFont].size.height; // NSLog(@"%f", h); self.placeholderLabel.frame = CGRectMake(3, 7, self.bounds.size.width, 20); // self.placeholderLabel.backgroundColor = [UIColor redColor]; //self.placeholderLabel.frame = CGRectMake(5, 7, self.bounds.size.width - 20, 20); } } - (void)setup { //添加一个通知,监听textview的内容是否改变 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:UITextViewTextDidChangeNotification object:self]; self.placeholderLabel = [[UILabel alloc]init]; // self.placeholderLabel.textAlignment = NSTextAlignmentCenter; // __weak LAXTextView *ws = self; // self.KVOController = [FBKVOController controllerWithObserver:self]; // [self.KVOController observe:self keyPath:@"frame" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld block:^(id observer, id object, NSDictionary *change) // { // [ws setPlaceholderFrame]; // }]; self.placeholder = @""; self.placeholderFont = self.font; self.placeholderColor= [UIColor grayColor]; self.placeholderLabel.backgroundColor = [UIColor clearColor]; self.placeholderLabel.text = _placeholder; self.placeholderLabel.numberOfLines = 0; [self addSubview:self.placeholderLabel]; self.font= [UIFont systemFontOfSize:14]; } #pragma mark - Notifications - (void)didReceiveTextDidChangeNotification:(NSNotification *)notification { [self setNeedsDisplay]; } - (void)notificationAction{ self.placeholderLabel.hidden = (self.hasText || self.hasText2); self.placeholderLabel.text = _placeholder; } @end
相关文章推荐
- ios UITextView 添加水印提示(Placeholder)
- 【iOS】UITextView中添加 placeholder
- UITextView 添加placeholder
- IOS UITextView加上placeholder
- iOS开发-带Placeholder的UITextView实现
- iOS开发之UITextView,设置textViewplaceholder
- 教大家如何给UITextView添加placeholder扩展
- 在UITextView中添加placeholder
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS开发-UITextView实现PlaceHolder的方式
- iOS开发之UITextView,设置textView的行间距及placeholder
- 给UITextView添加PlaceHolder
- ios--UITextView在光标处添加文字
- 关于如何在UITextView等添加输入提示(placeholder)
- iOS开发之UITextView,设置textView的行间距及placeholder
- iOS UITextView加上placeholder
- iOS 实现UITextView的placeholder
- 给UITextView添加类似placeholder功能
- iOS开发之UITextView,设置textView的行间距及placeholder
- 源码推荐(7.29):iOS图片查看器(多图全屏查看可缩放),给UITextView 添加超链接