zoukankan      html  css  js  c++  java
  • UITextView设置占位文字

    这里只介绍一种,这种方便扩展,可以占位文字颜色.

    我们继承一个UITextView:

    #import <UIKit/UIKit.h>
    
    @interface MyTextView : UITextView
    
    /** 占位文字 */
    @property (nonatomic, copy) NSString *placeholder;
    /** 占位文字颜色 */
    @property (nonatomic, strong) UIColor *placeholderColor;
    
    @end
    #import "MyTextView.h"
    
    @implementation MyTextView
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            // 设置默认字体
            self.font = [UIFont systemFontOfSize:15];
            
            // 设置默认颜色
            self.placeholderColor = [UIColor grayColor];
            
            // 使用通知监听文字改变
            [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
        }
        return self;
    }
    
    - (void)textDidChange:(NSNotification *)note
    {
        // 会重新调用drawRect:方法
        [self setNeedsDisplay];
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    /**
     * 每次调用drawRect:方法,都会将以前画的东西清除掉
     */
    - (void)drawRect:(CGRect)rect
    {
        // 如果有文字,就直接返回,不需要画占位文字
        if (self.hasText) return;
        
        // 属性
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        attrs[NSFontAttributeName] = self.font;
        attrs[NSForegroundColorAttributeName] = self.placeholderColor;
        
        // 画文字
        rect.origin.x = 5;
        rect.origin.y = 8;
        rect.size.width -= 2 * rect.origin.x;
        [self.placeholder drawInRect:rect withAttributes:attrs];
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        [self setNeedsDisplay];
    }
    
    #pragma mark - setter
    - (void)setPlaceholder:(NSString *)placeholder
    {
        _placeholder = [placeholder copy];
        
        [self setNeedsDisplay];
    }
    
    - (void)setPlaceholderColor:(UIColor *)placeholderColor
    {
        _placeholderColor = placeholderColor;
        
        [self setNeedsDisplay];
    }
    
    - (void)setFont:(UIFont *)font
    {
        [super setFont:font];
        
        [self setNeedsDisplay];
    }
    
    - (void)setText:(NSString *)text
    {
        [super setText:text];
        
        [self setNeedsDisplay];
    }
    
    - (void)setAttributedText:(NSAttributedString *)attributedText
    {
        [super setAttributedText:attributedText];
        
        [self setNeedsDisplay];
    }

    调用:

        MyTextView *my = [[MyTextView alloc] initWithFrame:CGRectMake(10, 44, [UIScreen mainScreen].bounds.size.width-20, 200)];
        my.placeholder = @"请输入文字";
        //不设置颜色,默认是灰色.
        my.placeholderColor = [UIColor orangeColor];
        //添加边框
        my.layer.borderColor = [UIColor grayColor].CGColor;
        
        my.layer.borderWidth =1.0;
        
        my.layer.cornerRadius =5.0;
        
        [self.view addSubview:my];
  • 相关阅读:
    55最佳实践系列:Logging最佳实践
    关于 Multiget hole:spymemcached对此的实现方法
    Java两则故障分析和常见连接超时时间
    spymemcached :某个mc节点操作连续超时超过998次就 AutoReconnect 的特性
    最佳实践系列:前端代码标准和最佳实践
    随手小记:PHPFPM模式下PHP最大执行时间、Pragma和postcheck
    Storm 是如何跟踪一条消息以及它衍生出来的消息都被成功处理的
    Tumblr的消息通知系统是如何构建的
    职业化之可以固化的六个工作模式
    三个实例演示 Java Thread Dump 日志分析
  • 原文地址:https://www.cnblogs.com/dianming/p/6732577.html
Copyright © 2011-2022 走看看