带属性的字符串(NSMutableAttributedString)的使用


    //带有属性的文字

    //1.创建对象

  NSString *original = @"今天你还好吗?";

  NSMutableAttributedString *attrTitle = [[NSMutableAttributedStringalloc] initWithString:original];

  //2.设置颜色(NSForegroundColorAttributeName代表要设置颜色, value代表值, range 代表范围)

  /**

    其他设置:

      1.NSForegroundColorAttributeName //颜色

      2.NSFontAttributeName //字体

      3.NSBackgroundColorAttributeName //背景色

     //还有其他的很多的属性,可以自己去看苹果的API,这里不再详述

  */

  [attrTitle addAttribute:NSForegroundColorAttributeName value:[UIColorblueColor] range:NSMakeRange(0, 2)];

 //3.添加到Label中

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

    label.frame = CGRectMake(100, 100, 100, 40);

    [label setAttributedText:attrTitle];

    [self.view addSubview:label];


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM