iOS给UITextView添加Placeholder
来源:互联网 发布:裁床捆条算法公式 编辑:程序博客网 时间:2024/05/16 05:33
//// 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
0 0
- iOS给UITextView添加Placeholder
- 给UITextView添加PlaceHolder
- 【iOS】UITextView中添加 placeholder
- 给UITextView添加类似placeholder功能
- ios UITextView 添加水印提示(Placeholder)
- iOS 给TextView添加placeholder.
- UITextView 添加placeholder
- UITextView添加Placeholder(swift)
- UITextView添加placeholder属性
- UITextView添加placeholder属性
- UITextView添加placeHolder
- UITextView添加placeholder属性
- 教大家如何给UITextView添加placeholder扩展
- DeveKing7-给UITextView添加Placeholder最简单易懂的方法
- IOS UITextView加上placeholder
- iOS UITextView加上placeholder
- IOS UITextView加上placeholder
- ios UITextView placeHolder效果
- MySql之多表查询
- Timer例子
- MySQL备忘之数据更新篇(INSERT, UPDATE, DELETE)
- LInux sed命令详解
- 记录项目分包结构
- iOS给UITextView添加Placeholder
- 位图索引BitMap
- ImportError cannot import name BytesIO when import caffe
- ubuntu平台vivado无法识别NIC_ID而无法使用license的解决办法
- Linux netstat命令详解
- 泛腾科技xStorage6110 6U VPX大容量存储系统
- NDK学习笔记2之捕获native层的段错误
- sparkStreaming+kafka python版wordcount申请资源不成功
- 浏览器无法连接的解决方法 windows无法与设备和资源通信的解决方法