iOS 如何優(yōu)雅的控制富文本換行間距

目前有個業(yè)務(wù)需求是實現(xiàn)如下圖兩個提示窗


image.png
需求

需要實現(xiàn)可復(fù)用的彈窗,一般中間部分的思路是使用控件一點點的相對布局,本文采用更優(yōu)雅的形式,中間整體部分用富文本實現(xiàn)

代碼如下
-(NSMutableAttributedString *)getAttrStringFromDic:(NSDictionary *)dic{
    NSString *firstTitle = dic[@"firstTitle"];
    NSString *firstDes = dic[@"firstDes"];
    NSString *secondTitle = dic[@"secondTitle"];
    NSArray *secondDesArr = dic[@"secondDesArr"];
    
//    NSString *firstTitle = @"課程理念";
//    NSString *firstDes = @"自然拼讀是母語為英語國家的小朋友學英語的一套發(fā)音方法,幫助孩子擺脫死記硬背單詞。課程系統(tǒng)學習26個字母和常見字母組合的發(fā)音規(guī)則,讓孩子見詞能讀,能聽能寫,提升記單詞的效率,同時提高閱讀能力,通過自然拼讀課程的學習可以解碼小學階段80%以上的單詞。";
//    NSString *secondTitle =@"預(yù)期效果";
//    NSArray *secondDesArr = @[@"1.正確區(qū)分26個字母對應(yīng)大小寫,并掌握26個字母",@"2.正確區(qū)分26個字母對應(yīng)大小寫,并掌握26個字母",@"3.正確區(qū)分26個字母對應(yīng)大小寫,并掌握26個字母"];
    
    // 小空行
    NSMutableAttributedString *space = [[NSMutableAttributedString alloc] initWithString:@" \n" attributes:@{
        NSForegroundColorAttributeName:RGBA(72, 106, 154),
        NSFontAttributeName:[UIFont systemFontOfSize:8],
    }];
    
    // 大空行
    NSMutableAttributedString *bigSpace = [[NSMutableAttributedString alloc] initWithString:@" \n" attributes:@{
        NSForegroundColorAttributeName:RGBA(72, 106, 154),
        NSFontAttributeName:[UIFont systemFontOfSize:20],
    }];
    
    // 最終字符
    NSMutableAttributedString *totalString = [[NSMutableAttributedString alloc] init];
    
    // 標題
    NSMutableAttributedString *firstTitleStr = [[NSMutableAttributedString alloc] initWithString:firstTitle?:@"" attributes:@{
        NSForegroundColorAttributeName:RGBA(18, 39, 69),
        NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
    }];
    [totalString appendAttributedString:firstTitleStr];
    
    [totalString appendAttributedString:space];
    [totalString appendAttributedString:space];
    
    // 描述
    NSMutableAttributedString *firstDesStr = [[NSMutableAttributedString alloc] initWithString:firstDes?:@"" attributes:@{
        NSForegroundColorAttributeName:RGBA(18, 39, 69),
        NSFontAttributeName:[UIFont systemFontOfSize:12],
    }];
    [totalString appendAttributedString:firstDesStr];
    
    // 增加小空行和大空行
    [totalString appendAttributedString:[space copy]];
    [totalString appendAttributedString:[bigSpace copy]];
    
    // 標題
    NSMutableAttributedString *secondTitleStr = [[NSMutableAttributedString alloc] initWithString:secondTitle?:@"" attributes:@{
        NSForegroundColorAttributeName:RGBA(18, 39, 69),
        NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
    }];
    [totalString appendAttributedString:secondTitleStr];
    
    // 增加空行
    [totalString appendAttributedString:[space copy]];
    [totalString appendAttributedString:[space copy]];
    
    // 描述字斷
    for (NSString *subtitle in secondDesArr) {
        NSMutableAttributedString *subtitleStr = [[NSMutableAttributedString alloc] initWithString:subtitle?:@"" attributes:@{
            NSForegroundColorAttributeName:RGBA(18, 39, 69),
            NSFontAttributeName:[UIFont systemFontOfSize:12],
        }];
        [totalString appendAttributedString:subtitleStr];
        [totalString appendAttributedString:[space copy]];
    }
    
    
    return totalString

這樣只用一個label的attributestring就能實現(xiàn)所有內(nèi)容的繪制。

技巧
  NSMutableAttributedString *space = [[NSMutableAttributedString alloc] initWithString:@" \n" attributes:@{
        NSForegroundColorAttributeName:RGBA(72, 106, 154),
        NSFontAttributeName:[UIFont systemFontOfSize:8],
    }];

利用此代碼實現(xiàn)換行的作用,行的高度可以用空格的字體大小來控制。優(yōu)雅的解決了需求。

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

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

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