借用runtime来实现UITextView的占位符placehold功能

1、 首先我们需要倒入runtime的头文件 #import <objc/runtime.h>

unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UITextView class], &count);
    for (int i = 0; i < count; i ++) {
        Ivar ivar = ivars[i];
        const char *name = ivar_getName(ivar);
        NSString *objcName = [NSString stringWithUTF8String:name];
        NSLog(@"%d : %@",i,objcName);
#warning 通过打印可以发现 uitextField 有 ‘_placeholderLabel’占位符属性
    }

打印结果
通过打印我没可以看到UITextField是存在placeholder属性的

  • 布局UITextField
- (void)setupTextView
{
    CGFloat margin = 15;
    UILabel *tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(margin, 0, SCREEN_width - 2 * margin, 50)];
    tipLabel.text = @"你的批评和建议能帮助我们更好的完善产品,请留下你的宝贵意见!";
    tipLabel.numberOfLines = 2;
    tipLabel.textColor = [UIColor colorWithRed:255 green:10 blue:10 alpha:1];
    tipLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:tipLabel];

    CGFloat height  = 200;
#ifndef __IPHONE_4_0
    height = 100;
#endif
    UITextView *iderTextView = [[UITextView alloc] initWithFrame:CGRectMake(margin, CGRectGetMaxY(tipLabel.frame) + margin, SCREEN_width - 2 * margin, height)];
    iderTextView.backgroundColor = [UIColor whiteColor];
    iderTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    iderTextView.layer.borderWidth = 1;
    iderTextView.scrollEnabled = YES;
    iderTextView.scrollsToTop = YES;
    iderTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    //    iderTextView.delegate = self;
    self.iderTextView = iderTextView;
    [self.view addSubview:iderTextView];

    // _placeholderLabel
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"请输入宝贵意见(300字以内)";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.font = [UIFont systemFontOfSize:14];
    placeHolderLabel.textColor = [UIColor lightGrayColor];
    [placeHolderLabel sizeToFit];
    [iderTextView addSubview:placeHolderLabel];
#warning 通过‘_placeholderLabel’属性来设置他的占位符
    [iderTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    iderTextView.font =  placeHolderLabel.font;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值