TextView自定义
来源:互联网 发布:济宁知豆电动汽车电话 编辑:程序博客网 时间:2024/05/20 18:20
#import <UIKit/UIKit.h>@interface HMTextView : UITextView@property (nonatomic, copy) NSString *placehoder;@property (nonatomic, strong) UIColor *placehoderColor;@end
#import "HMTextView.h"@interface HMTextView() <UITextViewDelegate>@property (nonatomic, weak) UILabel *placehoderLabel;@end@implementation HMTextView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; // 添加一个显示提醒文字的label(显示占位文字的label) UILabel *placehoderLabel = [[UILabel alloc] init]; placehoderLabel.numberOfLines = 0; placehoderLabel.backgroundColor = [UIColor clearColor]; [self addSubview:placehoderLabel]; self.placehoderLabel = placehoderLabel; // 设置默认的占位文字颜色 self.placehoderColor = [UIColor lightGrayColor]; // 设置默认的字体 self.font = [UIFont systemFontOfSize:14];#warning 不要设置自己的代理为自己本身 // 监听内部文字改变// self.delegate = self; /** 监听控件的事件: 1.delegate 2.- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 3.通知 */ // 当用户通过键盘修改了self的文字,self就会自动发出一个UITextViewTextDidChangeNotification通知 // 一旦发出上面的通知,就会调用self的textDidChange方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; } return self;}- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 监听文字改变- (void)textDidChange{// if (self.text.length == 0) { // 显示占位文字// self.placehoderLabel.hidden = NO;// } else { // 隐藏占位文字// self.placehoderLabel.hidden = YES;// }// text:属性 包含普通的文字字符串// attributedText:只要显示在textview里面所有的内容(表情,text) self.placehoderLabel.hidden = (self.attributedText.length != 0);}// 重写方法-(void)setAttributedText:(NSAttributedString *)attributedText {[super setAttributedText:attributedText];[self textDidChange];}#pragma mark - 公共方法- (void)setText:(NSString *)text{ [super setText:text]; [self textDidChange];}- (void)setPlacehoder:(NSString *)placehoder{#warning 如果是copy策略,setter最好这么写 _placehoder = [placehoder copy]; // 设置文字 self.placehoderLabel.text = placehoder; // 重新计算子控件的fame [self setNeedsLayout];}- (void)setPlacehoderColor:(UIColor *)placehoderColor{ _placehoderColor = placehoderColor; // 设置颜色 self.placehoderLabel.textColor = placehoderColor;}- (void)setFont:(UIFont *)font{ [super setFont:font]; self.placehoderLabel.font = font; // 重新计算子控件的fame [self setNeedsLayout];}- (void)layoutSubviews{ [super layoutSubviews]; self.placehoderLabel.y = 8; self.placehoderLabel.x = 5; self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.x; // 根据文字计算label的高度 CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT); CGSize placehoderSize = [self.placehoder sizeWithFont:self.placehoderLabel.font constrainedToSize:maxSize]; self.placehoderLabel.height = placehoderSize.height;}@end
使用
// 添加输入控件- (void)setupTextView{ // 1.创建输入控件 HMTextView *textView = [[HMTextView alloc] init]; textView.frame = self.view.bounds; [self.view addSubview:textView]; self.textView = textView; // 2.设置提醒文字(占位文字) textView.placehoder = @"分享新鲜事..."; // 3.设置字体 textView.font = [UIFont systemFontOfSize:15];}
1 0
- 自定义textview
- 自定义textview
- 自定义TextView
- 自定义TextView
- TextView自定义
- 自定义TextView
- 自定义textView
- 自定义TextView
- 自定义TextView
- 自定义TextView
- 自定义TextView 链接
- 自定义TextView字体
- [自定义控件学习]Textview
- android 自定义TextView
- android 自定义TextView
- 自定义TextView控件
- Android 自定义TextView超链接
- 自定义旋转TextView控件
- 九度oj--1002和1045
- 【单片机执行指令的过程】
- 开始使用Log4j
- 软件 易用性理解
- 137.Your database instance is currently configured to support 1,500 connections. The Web application
- TextView自定义
- jQuery添加/筛选/检测(3)
- C++string类常用函数 c++中的string常用函数用法总结
- PHP接口开发
- github优秀开源项目大全-iOS
- fork和pause等系统调用的实现原理(Linux0.11内核)
- Android武器库
- Spring使用之:Quartz定时任务为什么会被阻塞
- 前端安全