NSAttributedString 帶屬性的字符串

NSAttributedString 帶屬性的字符串

  • set都是改變之前的設置或取代同類.比如設置代理,通常代理只是一個對象屬性,重復設置,也只有一個,具體起作用看情況
  • add都是添加,補充之前的設置,或添加同類.比如添加target,添加消息,addOberver等等,都是添加到集合,每個添加的都有效
    ![image](images/屏幕快照 2015-11-09 15.25.16.png)
   // setAttributes:(字典),是設置改變Attributes字典指向(Attributes字典存字符串屬性的所有鍵值對),改變屬性字典指向了,之前設置的無效
   //addAttribute:(鍵值對)/addAttributes:(字典)往Attributes字典調(diào)加屬性設置的鍵值對或字典,未改變屬性字典指向,之前設置的非同一key的有效.
    //方法名規(guī)律
   // set都是改變之前的設置或取代同類.add都是添加,補充之前的設置,或添加同類
NSString:NSObject//無屬性字符串,不可變
NSAtrributedString:NSObject//帶屬性字符串
//屬性和字符串都不可變,一創(chuàng)建好就不可變

每個字符獨立接收屬性(range,批量設置范圍內(nèi)的所有字符,但每個字符獨立接收屬性)
重復設置同一字符(在同一字符串中位置相同),最后一次有效
  • 例子利用TextField的attributedPlaceholder來探討NSAttributedString
@property(nullable, nonatomic,copy)   NSAttributedString     *attributedPlaceholder;
  • NSAttributedString不可變.(類NSString)字符串和屬性在創(chuàng)建后,不可變.(用之創(chuàng)建新字符串不是)
帶有屬性的字符串, 富文本
由2部分組成
文字內(nèi)容 : NSString *
文字屬性 : NSDictionary *
文字顏色 - NSForegroundColorAttributeName
字體大小 - NSFontAttributeName
下劃線 - NSUnderlineStyleAttributeName
背景色 - NSBackgroundColorAttributeName
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
    attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
    attributes[NSUnderlineStyleAttributeName] = @YES;
    //創(chuàng)建新的屬性字符串,創(chuàng)建后不可變(非指針指向不可變,而是字符串本身和屬性不可變).所以創(chuàng)建時就要設置好屬性
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attributes];
  • NSMutableAttributedString可變(類NSMutableString)字符串和屬性在創(chuàng)建后,可變.
 ////創(chuàng)建新的屬性字符串,創(chuàng)建后可變,
  NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
  // 創(chuàng)建后改變屬性
  NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
  attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
  //1set
  [string setAttributes:attributes range:NSMakeRange(0, 1)];
  
  NSMutableDictionary *attributes2 = [NSMutableDictionary dictionary];
  attributes2[NSBackgroundColorAttributeName] = [UIColor redColor];
  attributes2[NSUnderlineStyleAttributeName] = @YES;
  
 // setAttributes:(字典),是設置改變Attributes字典指向(Attributes字典存字符串屬性的所有鍵值對),改變屬性字典指向了,之前設置的//1set無效,因為屬性作用是在每個字符的,//2setRange(0, 2)包含了//1setRange(0, 1)中的字符所以//1set被覆蓋了屬性
 
 //2set
  [string setAttributes:attributes2 range:NSMakeRange(0, 2)];
  
  //addAttribute:(鍵值對)/addAttributes:(字典)往Attributes字典調(diào)加屬性設置的鍵值對或字典,未改變屬性字典指向,之前設置的非同一key的有效.
  [string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 1)];
  [string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
  [string addAttribute:NSUnderlineStyleAttributeName value:@YES range:NSMakeRange(1, 1)];
  self.attributedPlaceholder = string;

  • UILabel利用
UILabel *label = [[UILabel alloc] init];
//    label.text = @"你好哈哈哈";
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"你好哈哈哈"];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 3)];
    [text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
    label.attributedText = text;
    label.frame = CGRectMake(100, 100, 100, 25);
    [self.view addSubview:label];

  • 一個lbale兩行字,不同屬性
    UILabel *label = [[UILabel alloc] init];
    // 設置屬性文字
    NSString *text = @"你好\n哈哈哈";
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
    [attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
    label.attributedText = attributedText;
    // 其他設置
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(0, 0, 100, 40);
    [self.view addSubview:label];
    self.navigationItem.titleView = label;
  • 利用屬性字符串,進行l(wèi)abel的圖文混排
  UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(100, 100, 200, 25);
    label.backgroundColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:label];

    // 圖文混排
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
    // 1 - 你好
    NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
    [attributedText appendAttributedString:first];

    // 2 - 圖片
    // 帶有圖片的附件對象
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"header_cry_icon"];
    CGFloat lineH = label.font.lineHeight;
    attachment.bounds = CGRectMake(0, - ((label.xmg_height - lineH) * 0.5 - 1), lineH, lineH);
    // 將附件對象包裝成一個屬性文字
    NSAttributedString *second = [NSAttributedString attributedStringWithAttachment:attachment];
    [attributedText appendAttributedString:second];

    // 3 - 哈哈哈
    NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"];
    [attributedText appendAttributedString:third];

    label.attributedText = attributedText;
    
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末塔拳,一起剝皮案震驚了整個濱河市南吮,隨后出現(xiàn)的幾起案子所坯,更是在濱河造成了極大的恐慌,老刑警劉巖富俄,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡亏钩,警方通過查閱死者的電腦和手機剑肯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進店門捧毛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人让网,你說我怎么就攤上這事呀忧。” “怎么了溃睹?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵而账,是天一觀的道長。 經(jīng)常有香客問我因篇,道長泞辐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任竞滓,我火速辦了婚禮咐吼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘商佑。我一直安慰自己锯茄,他們只是感情好,可當我...
    茶點故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布茶没。 她就那樣靜靜地躺著肌幽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪礁叔。 梳的紋絲不亂的頭發(fā)上牍颈,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天,我揣著相機與錄音琅关,去河邊找鬼煮岁。 笑死讥蔽,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的画机。 我是一名探鬼主播冶伞,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼步氏!你這毒婦竟也來了响禽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤荚醒,失蹤者是張志新(化名)和其女友劉穎芋类,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體界阁,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡侯繁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了泡躯。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贮竟。...
    茶點故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖较剃,靈堂內(nèi)的尸體忽然破棺而出咕别,到底是詐尸還是另有隱情,我是刑警寧澤写穴,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布惰拱,位于F島的核電站,受9級特大地震影響啊送,放射性物質(zhì)發(fā)生泄漏弓颈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一删掀、第九天 我趴在偏房一處隱蔽的房頂上張望翔冀。 院中可真熱鬧,春花似錦披泪、人聲如沸纤子。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽控硼。三九已至,卻和暖如春艾少,著一層夾襖步出監(jiān)牢的瞬間卡乾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工缚够, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留幔妨,地道東北人鹦赎。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像误堡,于是被迫代替她去往敵國和親古话。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理锁施,服務發(fā)現(xiàn)陪踩,斷路器,智...
    卡卡羅2017閱讀 134,628評論 18 139
  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 10,915評論 6 13
  • 字體屬性設置示例:if(color ==nil) {color = [NSColor redColor];}NSF...
    袏扌戒指閱讀 8,419評論 0 2
  • 我能做的就是安坐當下,傾聽窗外嘈雜的聲音掠過我的世界姥饰,等待靈感之神扔下的詞句婚温,我忠實地記錄下來。其它我無需再做媳否,也...
    緣在菩提閱讀 475評論 0 0
  • 周凡在胡莉莉出院以后就全身心的投入到了第三部小說的創(chuàng)作中步绸,他不分晝夜的把自己關在房間里掺逼,一天一頓飯,要外出頂多也就...
    趙貓魚閱讀 371評論 0 0