码迷,mamicode.com
首页 > 其他好文 > 详细

UITextView占位文字(placeHolder)

时间:2016-07-04 15:04:09      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

- (void)viewDidLoad {

    [super viewDidLoad];

 

    self.contentTV = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, APPSCREEN_WIDTH - 40, 100)];

    self.contentTV.layer.borderWidth = 0.5;

    self.contentTV.delegate = self;

    self.contentTV.attributedText = [self stringTurnToAttstrWithString:@"   *我要留言:"];

    self.contentTV.font = [UIFont systemFontOfSize:15];

    self.contentTV.layer.borderColor = [UIColor lightGrayColor].CGColor;

    [self.view addSubview:self.contentTV];

}

 

- (NSMutableAttributedString *)stringTurnToAttstrWithString:(NSString *)string{

    

    NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString:string];

    //颜色

    [attStr addAttribute:NSForegroundColorAttributeName

                   value:[UIColor redColor]

                   range:NSMakeRange(3, 1)];

    [attStr addAttribute:NSForegroundColorAttributeName

                   value:RGBCOLORV(0xaaaaaa)

                   range:NSMakeRange(4, string.length - 4)];

    //字体大小

    [attStr addAttribute:NSFontAttributeName

                   value:[UIFont systemFontOfSize:15]

                   range:NSMakeRange(0, attStr.length)];

    return attStr;

}

 

- (void)textViewDidBeginEditing:(UITextView *)textView{

    

    if ([textView.text isEqualToString:@"   *我要留言:"]) {

        

        textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@""];

    }

    textView.textColor = RGBCOLORV(0x000000);

    

}

 

- (void)textViewDidEndEditing:(UITextView *)textView{

    

    if (textView.attributedText.length == 0) {

        

        textView.attributedText = [self stringTurnToAttstrWithString:@"   *我要留言:"];

    }

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    

    UITouch *tc = [touches anyObject];

    if ([self.contentTV isFirstResponder] && tc.view != self.contentTV) {

        

        [self.contentTV resignFirstResponder];

    }

}

 

UITextView占位文字(placeHolder)

标签:

原文地址:http://www.cnblogs.com/Mr-------Li/p/5640442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!