iOS开发-自定义TextView

//

//  ZZTextView.h

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/10.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ZZTextView : UITextView

@property (nonatomic, copy) NSString *placeholder;

@property (nonatomic, strong) UIColor *placeholderColor;

@end

 

//

//  ZZTextView.m

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/10.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import "ZZTextView.h"

 

@interface ZZTextView()

@property (nonatomic, weak) UILabel *placeholderLabel;

 

@end

@implementation ZZTextView

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    

    if (self) {

        // 1.添加提示文字

        UILabel *placeholderLabel = [[UILabel alloc] init];

        placeholderLabel.textColor = [UIColor lightGrayColor];

        placeholderLabel.hidden = YES;

        placeholderLabel.numberOfLines = 0;

        placeholderLabel.backgroundColor = [UIColor clearColor];

        placeholderLabel.font = self.font;

        [self insertSubview:placeholderLabel atIndex:0];

        self.placeholderLabel = placeholderLabel;

        

        // 2.监听textView文字改变的通知

        [ZZNotificationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];

    }

    return self;

}

 

- (void)setPlaceholder:(NSString *)placeholder

{

#pragma warning  -----  NSString 赋值 一定要调用copy

    _placeholder = [placeholder copy];

    

    self.placeholderLabel.text = placeholder;

    if (placeholder.length) { // 需要显示

        self.placeholderLabel.hidden = NO;

        

        // 计算frame

        CGFloat placeholderX = 5;

        CGFloat placeholderY = 7;

        CGFloat maxW = self.frame.size.width - 2 * placeholderX;

        CGFloat maxH = self.frame.size.height - 2 * placeholderY;

        CGSize placeholderSize = [placeholder sizeWithFont:self.placeholderLabel.font constrainedToSize:CGSizeMake(maxW, maxH)];

        self.placeholderLabel.frame = CGRectMake(placeholderX, placeholderY, placeholderSize.width, placeholderSize.height);

    } else {

        self.placeholderLabel.hidden = YES;

    }

    //    self.placeholderLabel.hidden = (placeholder.length == 0);

}

 

- (void)setPlaceholderColor:(UIColor *)placeholderColor

{

    _placeholderColor = placeholderColor;

    

    self.placeholderLabel.textColor = placeholderColor;

}

 

- (void)setFont:(UIFont *)font

{

    [super setFont:font];

    

    self.placeholderLabel.font = font;

    self.placeholder = self.placeholder;

}

 

- (void)textDidChange

{

    self.placeholderLabel.hidden = (self.text.length != 0);

}

 

- (void)dealloc

{

    [ZZNotificationCenter removeObserver:self];

}

 

@end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值