利用NSMutableAttributedString屬性進行簡單的富文本,修改字體大小、字體顏色、文字開頭添加圖片、價格的中劃線

自己創建一個NSString的類別:
創建一個回調block:typedef void(^AttributedBlock)(id data);
1.根據需求頁面需要修改一個label里面的文字顏色及大小:

/**
 currentString   當前不需要改變的文字
 chageString     當前需要改變的文字
 endString       結尾字符串
 fontSize        改變的字體大小
 stringColor     改變的顏色
 tag 1:改變顏色。2:改變大小 3.改變顏色和字體
 */

/**改變字體顏色大小*/
+(void)stringWithCurrentString:(NSString *)currentString
              withChangeString:(NSString *)chageString
                     withColor:(UIColor *)stringColor
                       withTag:(NSInteger)tag
                      withFont:(NSInteger)fontSize
                 withEndString:(NSString *)endString
                     withBlock:(AttributedBlock)block;
+(void)stringWithCurrentString:(NSString *)currentString
              withChangeString:(NSString *)chageString
                     withColor:(UIColor *)stringColor
                       withTag:(NSInteger)tag
                      withFont:(NSInteger)fontSize
                 withEndString:(NSString *)endString
                     withBlock:(AttributedBlock)block
{
    NSString * textString = [NSString stringWithFormat:@"%@%@%@",currentString,chageString,endString];
    NSInteger currentStringLength = currentString.length;
    NSInteger chageStringLength = chageString.length;
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:textString];
    if (tag == 1)//改變顏色
    {
        [attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    else if (tag == 2)
    {
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize]    range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    else if (tag == 3)
    {
        [attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize]    range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    block(attributeString);
}

2.文字前面加圖片:

+(void)stringWithImage:(NSString *)currentString
             withBlock:(AttributedBlock)block;
+(void)stringWithImage:(NSString *)currentString
             withBlock:(AttributedBlock)block
{
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"需要添加的圖片"];
    attach.bounds = CGRectMake(0, -3, 15, 15);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    [attributeString insertAttributedString:attachString atIndex:0];
    block(attributeString);
}

3.顯示價格的時候需要顯示中劃線:

/**中劃線。價格*/
+(void)stringWithLine:(NSString *)currentString
            withBlock:(AttributedBlock)block;
+(void)stringWithLine:(NSString *)currentString
            withBlock:(AttributedBlock)block
{
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
    NSInteger currentStringLength = currentString.length;
    [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, currentStringLength)];
    [attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, currentStringLength)];
    block(attributeString);
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的數據,分別輸出它們 ...
    強子ly閱讀 3,026評論 8 46
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,217評論 30 472
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • { 11、核心動畫 需要簽協議,但是系統幫簽好 一、CABasicAnimation 1、創建基礎動畫對象 CAB...
    CYC666閱讀 1,591評論 2 4
  • 一、背景信息樓主在香港上學,非永久居民,具有港澳通行證逗留簽注,持中國護照。 今年暑假要去葡萄牙開會,會后計劃去西...
    鼴鼠素素閱讀 7,064評論 3 1