zoukankan      html  css  js  c++  java
  • UITextView占位文字(placeHolder)

    - (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];

        }

    }

  • 相关阅读:
    mysql 主键、复合主键等
    win7 增加edit with vim右键菜单
    设计模式学习1:简单工厂模式实现加减乘除等运算
    C#练习笔记3:分部类分部方法,索引结合异常的使用
    C#练习笔记4:枚举和数组练习
    C#练习笔记2:dll的编写与引用
    C#练习笔记1:自定义的类型转换和操作符的重载
    学习杂记1:c#,顺序泛型栈,泛型委托,Lambda,拓展方法
    数据结构学习日记3:顺序栈的实现(两种不同的方法)
    Unity中字幕的一个简单实现方式小结
  • 原文地址:https://www.cnblogs.com/Mr-------Li/p/5640442.html
Copyright © 2011-2022 走看看