帶屬性的字符串(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