zoukankan      html  css  js  c++  java
  • 带placeHolder 的textView

    #import <UIKit/UIKit.h>

    @interface CXTextView : UITextView

    @property (strong, nonatomic) NSString *placeHolder;

    @property (strong, nonatomic) UIColor *placeHolderColor;

    @end

    #import "CXTextView.h"

    @interface CXTextView ()

    @property (nonatomic, assign)BOOL isEdit;

    @end

    @implementation CXTextView

    - (id)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            // Initialization code

        }

        return self;

    }

    - (void)setPlaceHolder:(NSString *)placeHolder

    {

        _placeHolder = placeHolder;

        [self setNeedsDisplay];

    }

    - (void)setPlaceHolderColor:(UIColor *)placeHolderColor

    {

        _placeHolderColor = placeHolderColor;

        [self setNeedsDisplay];

    }

    - (BOOL)becomeFirstResponder

    {

        BOOL fist = [super becomeFirstResponder];

        if (fist) {

            self.isEdit = YES;

            [self setNeedsDisplay];

        }

        return fist;

    }

    - (BOOL)resignFirstResponder

    {

        BOOL resign = [super resignFirstResponder];

        if (resign) {

            self.isEdit = NO;

            [self setNeedsDisplay];

        }

        return resign;

    }

    - (void)drawRect:(CGRect)rect

    {

        if (_placeHolder && [self.text length] == 0 && !self.isEdit) {

            if (!_placeHolderColor) {

                [_placeHolderColor set];

            }else {

                [[UIColor grayColor] set];

            }

            UIFont *font = self.font;

            if (!font) font = [UIFont systemFontOfSize:17.f];

            [_placeHolder drawAtPoint:CGPointMake(5, 5) forWidth:rect.size.width withFont:font lineBreakMode:NSLineBreakByTruncatingTail];

        }

        [self.textColor set];

        [super drawRect:rect];

    }

    /*

    // Only override drawRect: if you perform custom drawing.

    // An empty implementation adversely affects performance during animation.

    - (void)drawRect:(CGRect)rect

    {

        // Drawing code

    }

    */

    @end

  • 相关阅读:
    20200302 数据分析之numpy以及Jupyter
    20200228 scrapy高级使用及分布式
    20200226 请求库selenium
    20200224 爬虫-requests模块
    0219 请求上下文解析与g对象
    Matlab矩阵求导和求梯度有什么不同
    矩阵中范数的处理方式
    如何巧妙地使用递归
    遗传算法求最短路径
    nparray的维度和取值方式
  • 原文地址:https://www.cnblogs.com/ldc529/p/3831125.html
Copyright © 2011-2022 走看看