iOS工程中對(duì)富文本的使用封裝

? 在iOS開發(fā)過(guò)程中八匠,經(jīng)常會(huì)用到很多酷炫的文本絮爷,比如說(shuō)文本中的某些文字要改變字號(hào)大小和文字顏色梨树,某些文字要支持點(diǎn)擊功能等坑夯,這些如果我們用Label文本是比較難以實(shí)現(xiàn)的,所以我們采用富文本的形式來(lái)實(shí)現(xiàn)這些功能抡四。代碼示例如下柜蜈,效果圖往下翻:

/**
 *  #pragma mark -- 富文本的封裝 --
 */
+ (NSMutableAttributedString*_Nullable)createMutableAttibutedStingWithPrefixStr:(NSString *_Nullable)prefixStr
        SuffixStr:(NSString *_Nullable)SuffixStr
 prefixStrColor:(UIColor *_Nullable)prefixStrColor
  SuffixStrColor:(UIColor *_Nullable)SuffixStrColor
   prefixStrFont:(CGFloat)prefixStrFont
   SuffixStrFont:(CGFloat)SuffixStrFont
{
NSMutableAttributedString *mutableAttributedStr = [NSMutableAttributedString new];

    // set family price
    NSMutableAttributedString *mutabPrefixStr
    = [[NSMutableAttributedString alloc]
       initWithString:prefixStr
       attributes:@{NSForegroundColorAttributeName : prefixStrColor,
                    NSFontAttributeName : [UIFont systemFontOfSize:prefixStrFont]}];
    NSMutableAttributedString *mutabSuffixStr
    = [[NSMutableAttributedString alloc]
       initWithString:SuffixStr
       attributes:@{NSForegroundColorAttributeName : SuffixStrColor,
                    NSFontAttributeName : [UIFont systemFontOfSize:SuffixStrFont]}];
    [mutabPrefixStr appendAttributedString:mutabSuffixStr];
    mutableAttributedStr = mutabPrefixStr;

    return mutableAttributedStr;
}

?上面是對(duì)于文字顏色和文字字號(hào)的設(shè)置,下面我們來(lái)看下文本中支持點(diǎn)擊的富文本是神馬樣子滴指巡?淑履!

/**
 *  #pragma mark -- 可點(diǎn)擊的富文本 --
 */
+(NSMutableAttributedString *_Nullable)setAttributedStringWithRecommedAtt:(NSString *_Nullable)recommedAtt
                                                         recommedAttibutes:(nullable NSDictionary<NSString *, id> *)recommedAttibutes
                                                                contentAtt:(NSString *_Nullable)contentAtt
                                                          contentAttibutes:(nullable NSDictionary<NSString *, id> *)contentAttibutes
                                                             rangeOfString:(NSString *_Nullable)rangeOfString
                                                        linkAttributeValue:(id _Nullable )linkAttributeValue

{
    NSMutableAttributedString *recommedAtti
    = [[NSMutableAttributedString alloc]initWithString:recommedAtt
                                            attributes:recommedAttibutes];
    NSMutableAttributedString *contentAtti = [[NSMutableAttributedString alloc]initWithString:contentAtt
                                                                                   attributes:contentAttibutes];
    
    //獲取需點(diǎn)擊的文字
    if ([rangeOfString isEqualToString:@""] == NO ||
        [rangeOfString isKindOfClass:[NSNull class]] == NO ||
        rangeOfString != nil)
    {
        NSRange range = [[contentAtti string] rangeOfString:rangeOfString];
        [contentAtti addAttribute:NSLinkAttributeName value:linkAttributeValue range:range];
    }
    
    [recommedAtti appendAttributedString:contentAtti];
    
    return recommedAtti;
}

?這個(gè)方法返回的富文本需配合使用UITextView使用,可點(diǎn)擊的文本需賦值給UITextView顯示藻雪,實(shí)現(xiàn)UItextView的這個(gè)代理:

- (BOOL)     textView:(UITextView*)textView
shouldInteractWithURL:(NSURL*)URL
              inRange:(NSRange)characterRange

?接下來(lái)我們?cè)倏聪赂晃谋局胁迦雸D片秘噪,是怎么實(shí)現(xiàn)的,代碼來(lái)也勉耀。指煎。蹋偏。

+(NSMutableAttributedString *)lableInsertImageByString:(NSString *)string
                                  needInsertImageNamed:(NSString *)imageNamed
                                           imageBounds:(CGRect)imageBounds
                                         insertAtIndex:(NSInteger)atIndex
{
    //文本中放置圖片
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
    // 添加圖片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 插入圖片
    attch.image = [UIImage imageNamed:imageNamed];
    // 設(shè)置圖片大小
    attch.bounds = imageBounds;//CGRectMake(0, -2, 22, 18);
    
    // 創(chuàng)建帶有圖片的富文本
    NSAttributedString *attStr = [NSAttributedString attributedStringWithAttachment:attch];
    
    //圖片插入文本中的位置
    [attri insertAttributedString:attStr atIndex:atIndex];
    return attri;

}

?以上是用于UILabel和UITextView的文本顯示的,下面我們?cè)賮?lái)按下UIButton上使用富文本至壤,構(gòu)造出來(lái)的是什么效果呢暖侨,期待。崇渗。字逗。往下看代碼實(shí)現(xiàn):

/**
 *  #pragma mark -- 自定義按鈕 --
 */
+ (void)customsButtonWithButton:(UIButton *)button
                       TopTitle:(NSString *_Nullable)topTitle
                    bottomTitle:(NSString *_Nullable)bottomTitle
                   topTitleFont:(CGFloat)topTitleFont
                bottomTitleFont:(CGFloat)bottomTitleFont
                  topTitleColor:(UIColor *_Nullable)topTitleColor
               bottomTitleColor:(UIColor *_Nullable)bottomTitleColor
                 titleAlignment:(NSTextAlignment)titleAlignment
                      imageName:(NSString *_Nullable)imageName
                    titleInsert:(UIEdgeInsets)titleInsert
                    imageInsert:(UIEdgeInsets)imageInsert
{
    NSMutableAttributedString *topMutableStr
    = [[NSMutableAttributedString alloc]
       initWithString:[NSString stringWithFormat:@"%@\n",topTitle]
       attributes:@{NSForegroundColorAttributeName : topTitleColor,
                    NSFontAttributeName : [UIFont systemFontOfSize:topTitleFont]}];
    NSMutableAttributedString *bottomMutableStr
    = [[NSMutableAttributedString alloc]
       initWithString:bottomTitle
       attributes:@{NSForegroundColorAttributeName : bottomTitleColor,
                    NSFontAttributeName : [UIFont systemFontOfSize:bottomTitleFont]}];
    
    [topMutableStr appendAttributedString:bottomMutableStr];
    
    [bottomTitle isEqualToString:@""]== NO ||
    [bottomTitle isKindOfClass:[NSNull class]] == NO ||
    bottomTitle != nil ? [button setAttributedTitle:topMutableStr forState:UIControlStateNormal]
                       : [button setTitle:topTitle forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    button.titleEdgeInsets = titleInsert;
    button.imageEdgeInsets = imageInsert;
    button.titleLabel.numberOfLines = 0;
    
    button.titleLabel.textAlignment = titleAlignment;
    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
}

?好了,大家如有好的想法和建議可留言相互交流學(xué)習(xí),奉上以上的實(shí)現(xiàn)效果如下:


代碼測(cè)試.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末宅广,一起剝皮案震驚了整個(gè)濱河市葫掉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌跟狱,老刑警劉巖俭厚,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異驶臊,居然都是意外死亡挪挤,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門关翎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)扛门,“玉大人,你說(shuō)我怎么就攤上這事纵寝÷壅” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵爽茴,是天一觀的道長(zhǎng)葬凳。 經(jīng)常有香客問(wèn)我,道長(zhǎng)室奏,這世上最難降的妖魔是什么火焰? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮胧沫,結(jié)果婚禮上昌简,老公的妹妹穿的比我還像新娘。我一直安慰自己琳袄,他們只是感情好江场,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著窖逗,像睡著了一般址否。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天佑附,我揣著相機(jī)與錄音樊诺,去河邊找鬼。 笑死音同,一個(gè)胖子當(dāng)著我的面吹牛词爬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播权均,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼顿膨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了叽赊?” 一聲冷哼從身側(cè)響起恋沃,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎必指,沒(méi)想到半個(gè)月后囊咏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡塔橡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年梅割,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片葛家。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡户辞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出惦银,到底是詐尸還是另有隱情咆课,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布扯俱,位于F島的核電站,受9級(jí)特大地震影響喇澡,放射性物質(zhì)發(fā)生泄漏迅栅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一晴玖、第九天 我趴在偏房一處隱蔽的房頂上張望读存。 院中可真熱鬧,春花似錦呕屎、人聲如沸让簿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)尔当。三九已至,卻和暖如春蹂安,著一層夾襖步出監(jiān)牢的瞬間椭迎,已是汗流浹背锐帜。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留畜号,地道東北人缴阎。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像简软,于是被迫代替她去往敵國(guó)和親蛮拔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355