TextView的封装和自定义
实现的效果如下:

#import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property (nonatomic , strong) UILabel *placeHolderLabel; // 默认的Label
@property (nonatomic , strong) NSString *placeholderStr; // 默认的文字显示
@property (nonatomic , strong) UIColor *palceHolderColor; //默认文字显示的颜色
@end
#import "CustomTextView.h"
@implementation CustomTextView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setPlaceholderStr:@""];
[self setPalceHolderColor:[UIColor lightGrayColor]];
}
return self;
}
// 接收数据
- (void)setPlaceholderStr:(NSString *)placeholderStr{
if (_placeholderStr != placeholderStr) {
_placeholderStr = placeholderStr;
// 防止创建多个
[self.placeHolderLabel removeFromSuperview];
self.placeHolderLabel = nil;
// 重新绘制 会调用drawRect方法
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
if (self.placeholderStr.length > ) {
if (_placeHolderLabel == nil) {
_placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , self.bounds.size.width - , )];
_placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
_placeHolderLabel.numberOfLines = ;
_placeHolderLabel.font = self.font;
_placeHolderLabel.backgroundColor = [UIColor clearColor];
_placeHolderLabel.textColor = self.palceHolderColor;
_placeHolderLabel.alpha = ;
_placeHolderLabel.tag = ;
[self addSubview:_placeHolderLabel];
}
_placeHolderLabel.text = self.placeholderStr;
//自适应宽高
[_placeHolderLabel sizeToFit];
}
if ([[self text] length] == && [[self placeholderStr] length] >) {
[[self viewWithTag:] setAlpha:1.0];
}
}
使用如下:
#import "Button1Controller.h" #import "CustomTextView.h" #define kTextBorderColor RGBCOLOR(227,224,216)
#undef RGBCOLOR
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1] @interface Button1Controller ()<UITextViewDelegate> @property (nonatomic,strong) CustomTextView *textView;
@property (nonatomic , strong) UIButton *commitButton; @end @implementation Button1Controller - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithRed:229.0/ green:229.0/ blue:229.0/ alpha:1.0f]; [self.view addSubview:self.textView]; self.automaticallyAdjustsScrollViewInsets = NO; [self.view addSubview:self.commitButton]; } // TextView - (CustomTextView *)textView{ if (!_textView) {
_textView = [[CustomTextView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width - , )];
_textView.backgroundColor = [UIColor whiteColor];
_textView.delegate = self;
_textView.font = [UIFont systemFontOfSize:.f];
_textView.textColor = [UIColor blackColor];
_textView.textAlignment = NSTextAlignmentLeft;
_textView.editable = YES;
_textView.layer.cornerRadius = 4.0f;
_textView.layer.borderColor = kTextBorderColor.CGColor;
_textView.layer.borderWidth = 0.5;
_textView.palceHolderColor = RGBCOLOR(0x89, 0x89, 0x89);
_textView.placeholderStr = @"请输入您的宝贵意见,我们会尽快处理!";
} return _textView; } // CommutButton - (UIButton *)commitButton{ if (!_commitButton) {
_commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
_commitButton.layer.cornerRadius = 2.0f;
_commitButton.frame = CGRectMake(, CGRectGetMaxY(self.textView.frame)+, self.view.frame.size.width - , );
_commitButton.backgroundColor = [self colorWithRGBHex:0x60cdf8];
[_commitButton setTitle:@"提交" forState:UIControlStateNormal];
[_commitButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
} return _commitButton; } // 16进制转颜色 - (UIColor *)colorWithRGBHex:(UInt32)hex
{
int r = (hex >> ) & 0xFF;
int g = (hex >> ) & 0xFF;
int b = (hex) & 0xFF; return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:1.0f];
} // 提交按钮被点击 - (void)sendFeedBack{ NSLog(@"提交..."); } // 判断如果用户输入\n,则收回键盘 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
} - (void)textViewDidBeginEditing:(UITextView *)textView{ self.textView.placeholderStr = @"";
}
TextView的封装和自定义的更多相关文章
- 使用xib封装一个自定义view的步骤
使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...
- OC - 31.通过封装的自定义布局快速实现商品展示
概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- C#封装程序集自定义类方法注释提示
一.为什么使用封装程序集: 在很多分布式应用程序开发中,针对每一种功能可能条用的接口不一样,往往习惯将需要被调用的接口,封装成DLL给调用方应用后使用,这样既规范了调用的方式,又避免了调用出现参数请求 ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个
1.这是一个自定义的TextView ,看吧,底下就是代码,应该都可以看懂,这里就不多说了 package com.example.admin.myutilsborder;import android ...
- iOS-AFNetworking封装Get(自定义HTTP Header)和Post请求及文件下载
前面提到AFNetworking是一个很强大的网络三方库,首先你需要引入AFNetworking三方库:如封装的有误还请指出,谢谢! 1.Get请求 /**Get请求 url 服务器请求地址 succ ...
- NoHttp封装--02 自定义请求
bean实体类请求: 1.bean import java.io.Serializable; import com.alibaba.fastjson.annotation.JSONField; pub ...
- mybatis二(参数处理和map封装及自定义resultMap)
.单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...
随机推荐
- xfs 文件系统
centos7.0开始默认文件系统是xfs,centos6是ext4,centos5是ext3 ext3和ext4的最大区别在于,ext3在fsck时需要耗费大量时间(文件越多,时间越长),而ext4 ...
- spring timetask 定时任务调度
作者:Garry1115 定时任务调度即在设置的特定时间执行特定的任务,不需要人工干预. spring timertask spring 自身所带定时任务类,不需要引入第三方jar包,使用方式如下: ...
- 基于SSM框架的通用权限框架设计
1. 整体解决方案概述 1.1 权限整体解决方案概述 权限设计主要有一下几大部分组成: PassPort: 针对现在系统的分析,系统之间有部分信息是共享的,这部分信息将由 ...
- list交集
在C#语言程序设计中,List集合是常用的集合数据类型,在涉及集合类型的运算中,有时候我们需要计算2个List集合中共有的数据,即对2个List集合求交集运算.此时可以使用C#语言提供的Interse ...
- 洛谷P1273 有线电视网【树形dp】
题目:https://www.luogu.org/problemnew/show/P1273 题意:一棵树,叶子节点是用户,每天边有一个权值表示花费,每一个用户有一个值表示他们会交的钱. 问在不亏本的 ...
- C# Stopwatch 使用
static IEnumerable<int> SampleData() { ; var r = new Random(); , arraySize).Select(x => r.N ...
- [Google Guava] 排序: Guava强大的”流畅风格比较器”
原文链接 译者: 沈义扬 排序器[Ordering]是Guava流畅风格比较器[Comparator]的实现,它可以用来为构建复杂的比较器,以完成集合排序的功能. 从实现上说,Ordering实例就是 ...
- rxjs入门指南
使用场景 在复杂的,频繁的异步请求场景,使用rxjs. 在依赖的多个异步数据,决定渲染的情景,使用rxjs. 总之:在前台频繁的.大量的.和后台数据交互的复杂项目里面,使用rxjs(web端,iOS, ...
- python3实现互信息和左右熵的新词发现--基于字典树
字典树 原来讲明白了剩下的就是具体实现了,最适合存储和计算词频的数据结构就是字典树,这里给一个讲解的很清楚的链接 具体代码 代码已开源,需要的点击这个Github
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度(SDUT 2804)
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char data ...