TextKit 實(shí)現(xiàn)文本分頁

TextKit的一些介紹在這里就不多說了,相關(guān)的信息可以看這里。這篇文章主要講文本分頁和遇到的問題。
TextKit中主要涉及的就三個(gè)類,實(shí)現(xiàn)分頁也用到這是三個(gè)。

NSTextStorage                
NSLayoutManager            
NSTextContainer              

主要的思路就是:

  1. NSTextStorage 存儲要分段的文本信息
  2. NSTextStorage 設(shè)置 NSLayoutManager
  3. 在NSLayoutManager 中添加 NSTextContainer
  4. 獲取NSTextContainer 顯示的文本長度
  5. 返回分段信息

這個(gè)思路跟CoreText的分段思路差不多,都是獲取一定范圍內(nèi)的可見文字的rang,

//TextKit 分頁
+ (NSArray *)pagingwithContentString:(NSString *)contentString contentSize:(CGSize)contentSize textAttribute:(NSDictionary *)textAttribute {
    NSMutableArray *pagingArray = [NSMutableArray array];
    NSMutableAttributedString *orginAttString = [[NSMutableAttributedString alloc] initWithString:contentString attributes:textAttribute];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:orginAttString];
    NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    while (YES) {
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:contentSize];
        [layoutManager addTextContainer:textContainer];
        NSRange rang = [layoutManager glyphRangeForTextContainer:textContainer];
        if (rang.length <= 0) {
            break;
        }
        NSString *str = [contentString substringWithRange:rang];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str attributes:textAttribute];
        [pagingArray addObject:attStr];
    }
    return pagingArray;
}

分頁問題解決了,但是在文本渲染的時(shí)候卻出現(xiàn)了問題。
對于TextView來時(shí),設(shè)置富文本有兩個(gè)方式,一種是通過NSTextStorage設(shè)置,一種是直接用attributedText設(shè)置,但問題來了同樣的NSMutableAttributedString,兩種方式渲染出來的效果居然不一樣。

左邊為attributedText設(shè)置,右邊為NSTextStorage設(shè)置

attributedText設(shè)置的行距會比NSTextStorage設(shè)置的行距大。這就導(dǎo)致用TextKit分頁的結(jié)果,用attributedText來設(shè)置渲染就會出現(xiàn)顯示不全的問題。但是如果文本為英文就沒問題。

兩邊的行距一致

再經(jīng)過一番研究后發(fā)現(xiàn)這是字體造成的,中文如果用<code>[UIFont systemFontOfSize:20]</code>就會出現(xiàn)這種問題,只要換了字體就行了。其中在系統(tǒng)提供的字體中,能夠完美使用的有下面幾個(gè),

Heiti SC              黑體-簡
Heiti TC              黑體-繁
PingFang TC           平方-簡
PingFang HK           平方-繁
PingFang SC           平方-繁

</br>
提供一個(gè)簡單的demo,寫的比較簡單,不過能說明問題


CoreText分頁

下面也提供一下CoreText的分頁方法,畢竟CoreText分頁的速度會快一點(diǎn),同樣在設(shè)置字體的時(shí)候,中文內(nèi)容要用<code>Heiti</code>或<code>PingFang</code>

//CoreText 分頁
+ (NSArray *)coreTextPaging:(NSAttributedString *)str textFrame:(CGRect)textFrame{
    NSMutableArray *pagingResult = [NSMutableArray array];
    CFAttributedStringRef cfAttStr = (__bridge CFAttributedStringRef)str;
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(cfAttStr);
    CGPathRef path = CGPathCreateWithRect(textFrame, NULL);

    int textPos = 0; 
    NSUInteger strLength = [str length];
    while (textPos < strLength)  {
        //設(shè)置路徑
        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), path, NULL);
        //生成frame
        CFRange frameRange = CTFrameGetVisibleStringRange(frame);
        NSRange ra = NSMakeRange(frameRange.location, frameRange.length);

       //獲取范圍并轉(zhuǎn)換為NSRange,然后以NSAttributedString形式保存
        [pagingResult addObject:[str attributedSubstringFromRange:ra]];
        
        //移動當(dāng)前文本位置
        textPos += frameRange.length;
       
        CFRelease(frame);
    }
    CGPathRelease(path);
    CFRelease(framesetter);
    return pagingResult;
}

簡單的demo:demo地址

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

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

  • 轉(zhuǎn)載:https://yq.aliyun.com/articles/60173 https://objccn.io...
    F麥子閱讀 3,023評論 0 2
  • 廈門路小學(xué)四年級三班宿琳棋爸爸 今天是傳統(tǒng)的七夕節(jié),帶著兒子回家看望父母。剛一進(jìn)門就看到父親在菜園干活,我立刻下手...
    宿孟豹閱讀 183評論 0 0
  • 江山伴畫淡定坐, 雛鳳亮麗唱中國 。 注 春華秋實(shí)卅載情, 一帶一路萬里歌。 ——《青年女歌手》(油畫:靳尚誼) ...
    珠江潮平閱讀 2,754評論 45 38
  • 居延遺址分布在今內(nèi)蒙古自治區(qū)額濟(jì)納旗和甘肅省金塔縣的境內(nèi)。指的是包括漢代張掖郡居延、肩水兩都尉所轄邊塞上的...
    塞外蒼鷹閱讀 2,240評論 0 1

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