ios富文本處理

在處理富文本的時(shí)候總是被一大長(zhǎng)串代碼嚇到,這篇主要介紹一種鏈?zhǔn)教幚矸椒ǎ幚砀晃谋究梢院?jiǎn)化不少代碼。PS:我也是邊學(xué)邊用,也算個(gè)筆記吧。

我們使用NSMutableAttributedString,給他添加一個(gè)分類(lèi),加一個(gè)方法即可

.h文件

#import <Foundation/Foundation.h>

@interface NSMutableAttributedString (HDAttribute)

- (NSMutableAttributedString *(^)(NSString *,NSDictionary<NSString *,id>*))add;

@end

FOUNDATION_EXTERN NSString *const NSImageAttributeName;

FOUNDATION_EXTERN NSString *const NSImageBoundsAttributeName;

.m文件

#import "NSMutableAttributedString+HDAttribute.h"

@implementation NSMutableAttributedString (HDAttribute)

- (NSMutableAttributedString *(^)(NSString *,NSDictionary<NSString *,id>*))add

{

? ? return ^NSMutableAttributedString *(NSString *string,NSDictionary<NSString *,id>*attrDic)

? ? {

? ? ? ? NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:attrDic];

? ? ? ? for (id key in [attributes allKeys])

? ? ? ? {

? ? ? ? ? ? if ([key isKindOfClass:[NSString class]])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if ([key isEqualToString:@"color"])

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? [attributesremoveObjectForKey:key];

? ? ? ? ? ? ? ? ? ? [attributessetValue:[attrDic valueForKey:key] forKey:NSForegroundColorAttributeName];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if ([key isEqualToString:@"font"])

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? [attributesremoveObjectForKey:key];

? ? ? ? ? ? ? ? ? ? [attributessetValue:[attrDic valueForKey:key] forKey:NSFontAttributeName];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? if ([[attributes allKeys] containsObject:NSImageAttributeName] || [[attributes allKeys] containsObject:NSImageBoundsAttributeName])

? ? ? ? {

? ? ? ? ? ? NSTextAttachment *attach = [[NSTextAttachment alloc]initWithData:nil ofType:nil];

? ? ? ? ? ? CGRect rect = CGRectFromString(attributes[NSImageBoundsAttributeName]);

? ? ? ? ? ? attach.bounds = rect;

? ? ? ? ? ? attach.image = attributes[NSImageAttributeName];

? ? ? ? ? ? [self appendAttributedString:[NSAttributedString attributedStringWithAttachment:attach]];

? ? ? ? }else

? ? ? ? {

? ? ? ? ? ? [self appendAttributedString:[[NSAttributedString alloc]initWithString:string attributes:attributes]];

? ? ? ? }

? ? ? ? return self;

? ? };


}

@end

NSString *const NSImageAttributeName = @"NSImageAttributeName";

NSString *const NSImageBoundsAttributeName = @"NSImageBoundsAttributeName";

調(diào)用

myLabel.attributedText = [NSMutableAttributedString new].add(@"一段文本",@{@"color":[UIColor greenColor],@"font":[UIFont systemFontOfSize:12]}).add(@"第二段文本",@{@"color":[UIColor redColor],@"font":[UIFont systemFontOfSize:13]});

1.為了使方法可以使用.調(diào)用,就能不傳參,這里巧妙的使用了返回block的形式,block本身有返回值并且傳參,調(diào)用處相當(dāng)于執(zhí)行block,相當(dāng)巧妙的處理方式。

參考鏈接?http://www.itdecent.cn/p/299207775531

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載:http://blog.csdn.net/u010330109/article/details/518821...
    F麥子閱讀 4,332評(píng)論 0 3
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,237評(píng)論 8 265
  • { 24、Sqlite數(shù)據(jù)庫(kù) 1、存儲(chǔ)大數(shù)據(jù)量,增刪改查,常見(jiàn)管理系統(tǒng):Oracle、MSSQLServer、DB...
    CYC666閱讀 1,050評(píng)論 0 1
  • 在孩子心里世上只有媽媽好,是一首歌,是一副畫(huà),芩寶最喜歡"我愛(ài)媽媽"
    馨婇閱讀 172評(píng)論 0 0
  • 冷風(fēng)襲擾, 最忌離床早。 樓外枇杷開(kāi)正好, 真?zhèn)€讓人煩惱。 莫笑單枕孤眠, 誰(shuí)人不懼天寒。 怎奈好花待賞, 一身只...
    G芬芳閱讀 224評(píng)論 0 4

友情鏈接更多精彩內(nèi)容