• 带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

  • 相关阅读:
    python技巧31[移植python2.x到3.x]
    python转载[正则表达式指南]
    Linux守护进程(init.d和xinetd)
    系统host文件的使用
    python技巧31[python文件的encoding和str的decode]
    python类库26[读写mysql]
    Windows批处理中文件的属性
    python类库31[读取环境变量]
    python基础31[python内置函数列表]
    文件共享和存储服务器
  • 原文地址:https://www.cnblogs.com/ldc529/p/3831125.html
Copyright © 2020-2023  润新知