zoukankan      html  css  js  c++  java
  • TextView的封装和自定义

    实现的效果如下:

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface CustomTextView : UITextView
    4 
    5 @property (nonatomic , strong) UILabel *placeHolderLabel; // 默认的Label
    6 @property (nonatomic , strong) NSString *placeholderStr;  // 默认的文字显示
    7 @property (nonatomic , strong) UIColor *palceHolderColor;  //默认文字显示的颜色
    8 @end
     1 #import "CustomTextView.h"
     2 
     3 @implementation CustomTextView
     4 
     5 - (instancetype)initWithFrame:(CGRect)frame{
     6     
     7     self = [super initWithFrame:frame];
     8     if (self) {
     9         [self setPlaceholderStr:@""];
    10         [self setPalceHolderColor:[UIColor lightGrayColor]];
    11         
    12     }
    13     return self;
    14 }
    15 
    16 // 接收数据
    17 
    18 - (void)setPlaceholderStr:(NSString *)placeholderStr{
    19     
    20     if (_placeholderStr != placeholderStr) {
    21         
    22         _placeholderStr = placeholderStr;
    23         
    24         // 防止创建多个
    25         
    26         [self.placeHolderLabel removeFromSuperview];
    27         self.placeHolderLabel = nil;
    28         
    29         // 重新绘制  会调用drawRect方法
    30         
    31         [self setNeedsDisplay];
    32     }
    33 }
    34 
    35 - (void)drawRect:(CGRect)rect{
    36     
    37     [super drawRect:rect];
    38     if (self.placeholderStr.length > 0) {
    39         
    40         if (_placeHolderLabel == nil) {
    41             _placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, self.bounds.size.width - 16, 0)];
    42             _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
    43             _placeHolderLabel.numberOfLines = 0;
    44             _placeHolderLabel.font = self.font;
    45             _placeHolderLabel.backgroundColor = [UIColor clearColor];
    46             _placeHolderLabel.textColor = self.palceHolderColor;
    47             _placeHolderLabel.alpha = 0;
    48             _placeHolderLabel.tag = 999;
    49             [self addSubview:_placeHolderLabel];
    50         }
    51         _placeHolderLabel.text = self.placeholderStr;
    52         
    53         //自适应宽高
    54         [_placeHolderLabel sizeToFit];
    55         
    56     }
    57     if ([[self text] length] == 0 && [[self placeholderStr] length] >0) {
    58         [[self viewWithTag:999] setAlpha:1.0];
    59     }
    60 
    61 }

    使用如下:

      1 #import "Button1Controller.h"
      2 
      3 #import "CustomTextView.h"
      4 
      5 #define kTextBorderColor     RGBCOLOR(227,224,216)
      6 #undef  RGBCOLOR
      7 #define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
      8 
      9 
     10 @interface Button1Controller ()<UITextViewDelegate>
     11 
     12 @property (nonatomic,strong) CustomTextView *textView;
     13 @property (nonatomic , strong) UIButton *commitButton;
     14 
     15 @end
     16 
     17 @implementation Button1Controller
     18 
     19 - (void)viewDidLoad {
     20     [super viewDidLoad];
     21 
     22     self.view.backgroundColor = [UIColor colorWithRed:229.0/255 green:229.0/255 blue:229.0/255 alpha:1.0f];
     23     
     24     [self.view addSubview:self.textView];
     25     
     26     self.automaticallyAdjustsScrollViewInsets = NO;
     27     
     28     [self.view addSubview:self.commitButton];
     29 
     30 
     31 }
     32 
     33 
     34 // TextView
     35 
     36 - (CustomTextView *)textView{
     37     
     38     if (!_textView) {
     39         _textView = [[CustomTextView alloc]initWithFrame:CGRectMake(20, 84, self.view.frame.size.width - 40, 180)];
     40         _textView.backgroundColor = [UIColor whiteColor];
     41         _textView.delegate = self;
     42         _textView.font = [UIFont systemFontOfSize:14.f];
     43         _textView.textColor = [UIColor blackColor];
     44         _textView.textAlignment = NSTextAlignmentLeft;
     45         _textView.editable = YES;
     46         _textView.layer.cornerRadius = 4.0f;
     47         _textView.layer.borderColor = kTextBorderColor.CGColor;
     48         _textView.layer.borderWidth = 0.5;
     49         _textView.palceHolderColor = RGBCOLOR(0x89, 0x89, 0x89);
     50         _textView.placeholderStr = @"请输入您的宝贵意见,我们会尽快处理!";
     51     }
     52     
     53     return _textView;
     54 
     55     
     56 }
     57 
     58 // CommutButton
     59 
     60 - (UIButton *)commitButton{
     61     
     62     if (!_commitButton) {
     63         _commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
     64         _commitButton.layer.cornerRadius = 2.0f;
     65         _commitButton.frame = CGRectMake(40, CGRectGetMaxY(self.textView.frame)+20, self.view.frame.size.width - 80, 40);
     66         _commitButton.backgroundColor = [self colorWithRGBHex:0x60cdf8];
     67         [_commitButton setTitle:@"提交" forState:UIControlStateNormal];
     68         [_commitButton addTarget:self action:@selector(sendFeedBack) forControlEvents:UIControlEventTouchUpInside];
     69     }
     70     
     71     return _commitButton;
     72     
     73 
     74 }
     75 
     76 // 16进制转颜色
     77 
     78 - (UIColor *)colorWithRGBHex:(UInt32)hex
     79 {
     80     int r = (hex >> 16) & 0xFF;
     81     int g = (hex >> 8) & 0xFF;
     82     int b = (hex) & 0xFF;
     83     
     84     return [UIColor colorWithRed:r / 255.0f
     85                            green:g / 255.0f
     86                             blue:b / 255.0f
     87                            alpha:1.0f];
     88 }
     89 
     90 // 提交按钮被点击
     91 
     92 - (void)sendFeedBack{
     93     
     94     NSLog(@"提交...");
     95     
     96 }
     97 
     98 // 判断如果用户输入
    ,则收回键盘
     99 
    100 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    101     
    102     if ([text isEqualToString:@"
    "]) {
    103         [textView resignFirstResponder];
    104         return NO;
    105     }
    106     return YES;
    107 }
    108 
    109 - (void)textViewDidBeginEditing:(UITextView *)textView{
    110     
    111     self.textView.placeholderStr = @"";
    112 }
  • 相关阅读:
    浅谈常量
    运算符
    TTL与CMOS门电路
    16位CRC校验_Delphi
    DXP快捷键记录(红色为经常用到的)
    论EFMS模拟量部分采集电路的修改
    稳压二极管应用电路_转载
    TVS二极管
    TryEnterCriticalSection___Delphi
    很好用的一个翻译插件
  • 原文地址:https://www.cnblogs.com/pengsi/p/5564389.html
Copyright © 2011-2022 走看看