自定义UITextView
来源:互联网 发布:php web服务的搭建 编辑:程序博客网 时间:2024/06/15 07:04
这里我们创建一个继承自UIView,内嵌UITextView的类
- HJTextView.h
//// HJTextView.h// HJTextView//// Created by 黄健 on 16/5/27.// Copyright © 2016年 黄健. All rights reserved.//#import <UIKit/UIKit.h>@interface HJTextView : UIView@property (nonatomic, assign) int maxLength;// 最大字数@property (nonatomic, retain) UIFont *font;// 字体大小,默认14@property (nonatomic, retain) UIColor *textColor;// 字体颜色@property (nonatomic, retain) UIColor *placeholderColor;// 提示文字颜色@property (nonatomic) UIEdgeInsets contentEdgeInsets;// 内间距@property (nonatomic, copy) NSString *text;// 文本域内容@property (nonatomic, copy) NSString *placeholder;// 提示文字@end
- HJTextView.m
//// HJTextView.m// HJTextView//// Created by 黄健 on 16/5/27.// Copyright © 2016年 黄健. All rights reserved.//#import "HJTextView.h"// 视图的宽高#define kWidth self.bounds.size.width#define kHeight self.bounds.size.height// 默认内间距#define kPadding 2// 文本域宽度#define kTextViewWidth _textView.bounds.size.width@interface HJTextView() <UITextViewDelegate>{ @private UITextView *_textView;// 内嵌文本域 UILabel *_tipLabel;// 提示文字标签 UILabel *_numLabel;// 字数限制标签}@end@implementation HJTextView- (instancetype)initWithFrame:(CGRect)frame{ if (self == [super initWithFrame:frame]) { [self initView]; } return self;}- (void)initView{ self.backgroundColor = [UIColor whiteColor]; _textView = [[UITextView alloc] init]; _textView.font = [UIFont systemFontOfSize:14]; _textView.frame = CGRectMake(kPadding, kPadding, kWidth - 2 * kPadding, kHeight - 2 * kPadding); _textView.delegate = self; _textView.backgroundColor = [UIColor clearColor]; [self addSubview:_textView];}- (void)setFrame:(CGRect)frame{ [super setFrame:frame]; [self initView]; [self setNeedsDisplay];}- (void)setMaxLength:(int)maxLength{ _maxLength = maxLength; _numLabel = [[UILabel alloc] init]; _numLabel.font = [UIFont systemFontOfSize:13]; CGFloat w = 100; CGFloat h = _numLabel.font.lineHeight + 2; CGFloat x = kWidth - w - 7; CGFloat y = kHeight - h - 5; _numLabel.frame = CGRectMake(x, y, w, h); _numLabel.textAlignment = NSTextAlignmentRight; _numLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1.0]; [self addSubview:_numLabel]; _numLabel.text = [NSString stringWithFormat:@"0/%d", maxLength];}- (void)setTextColor:(UIColor *)textColor{ _textColor = textColor; _textView.textColor = textColor;}- (void)setPlaceholderColor:(UIColor *)placeholderColor{ _placeholderColor = placeholderColor; _tipLabel.textColor = _placeholderColor; _numLabel.textColor = _placeholderColor;}- (void)setPlaceholder:(NSString *)placeholder{ _placeholder = placeholder; _tipLabel = [[UILabel alloc] init]; _tipLabel.font = _textView.font; _tipLabel.text = _placeholder; _tipLabel.textColor = _placeholderColor ? _placeholderColor : [UIColor colorWithWhite:0.8 alpha:1.0]; _tipLabel.numberOfLines = 0; [_textView addSubview:_tipLabel]; [_textView sendSubviewToBack:_tipLabel]; CGSize contentSize = [self contentSize:_placeholder withFont:_tipLabel.font andMaxWith:kTextViewWidth - 5]; _tipLabel.frame = CGRectMake(5, 8, contentSize.width, contentSize.height);}- (CGSize)contentSize:(NSString *)content withFont:(UIFont *)font andMaxWith:(CGFloat)maxWidth{ return [content boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;}- (void)setText:(NSString *)text{ _textView.text = text; _tipLabel.hidden = text.length > 0;}- (NSString *)text{ return _textView.text;}- (void)setFont:(UIFont *)font{ _textView.font = font; _tipLabel.font = font;}- (void)setContentEdgeInsets:(UIEdgeInsets)contentEdgeInsets{ CGFloat x = contentEdgeInsets.left; CGFloat y = contentEdgeInsets.top; CGFloat w = kWidth - x - contentEdgeInsets.right; CGFloat h = kHeight - y - contentEdgeInsets.bottom; _textView.frame = CGRectMake(x, y, w, h);}- (void)textViewDidChange:(UITextView *)textView{ _tipLabel.hidden = textView.text.length > 0; _numLabel.text = [NSString stringWithFormat:@"%ld/%d", textView.text.length, _maxLength];}- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ NSInteger length = textView.text.length - range.length + text.length; return length <= _maxLength;}@end
- 测试
HJTextView *hjTextView = [[HJTextView alloc] initWithFrame:CGRectMake(20, 50, screenW - 45, 90)];[self.view addSubview:hjTextView];hjTextView.font = [UIFont systemFontOfSize:14];hjTextView.maxLength = 20000;hjTextView.contentEdgeInsets = UIEdgeInsetsMake(2, 5, 25, 5);hjTextView.placeholder = @"请输入内容";
- 运行
0 0
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView
- 自定义UITextView控件
- 使用UITextView的问题及自定义UITextView
- iphone自定义UITextView的placeholder
- UITextView使用自定义表情键盘
- iOS学习之----------自定义UITextView
- UITextView自定义封装(带placeHolder)
- 自定义 UITextView 的 placeholder 属性
- UITextView自定义键盘和系统键盘
- UItextView自定义其高度,禁止滚动
- TextViewPlaceholder 自定义UITextView Placeholder文字颜色、大小
- ios-UITextview自定义 带placeholder,left间距
- UITextView
- UITextView
- UITextView
- 2dx-lua图片局部置灰
- UVA 839 Not so Mobile
- pooling mean max 前向和反向传播
- 物惯(子到父节点)变换顺序原因和不同坐标系下的变换顺序详解
- 简单结构体排序
- 自定义UITextView
- ecshop 后台增加设置选项(系统控制变量)的方法____2016年5月28日
- openwrt编译日志
- 记一次作为主讲人的培训经历
- XML解析之PULL解析
- Ubuntu 更改mac地址
- 一些关于内存缓冲区溢出的保护机制
- 蓝牙BLE ATT剖析(一)
- mac parallels