CoreText使用介绍
生活随笔
收集整理的這篇文章主要介紹了
CoreText使用介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、概述
??
1.CoreText是蘋果創建的一個用于文字排版的框架,可以實現文字排版、圖文混排等復雜的界面效果。從iOS3.2啟用。
2.一個開源工具類-OHAttributedLabel,就是使用CoreText框架實現的,能夠實現一個Label中有不同的文字大小、文字顏色、字體以及鏈接等。 ?
二、一般使用步驟
1.創建NSMutableAttributedString NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:contentString]; ? ? ? ? ? ? ? ?? 2.設置文字顏色 ? ? ? ? ? ? ? ? [attributeString addAttribute:(id)kCTForegroundColorAttributeName ? ? ? ? ? ? ? ? ? ? ? ? value:(id)[UIColor darkGrayColor].CGColor? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, tempArticle.desc.length)];
2.設置字體以及大小 ? ? ? ? ? ? ? ? CTFontRef font = CTFontCreateWithName(CFSTR("Bodoni 72"), contentFontSize, NULL); [attributeString addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, [attributeString length])]; CFRelease(font);
4.初始化段落首行縮進樣式 CGFloat headIndent = contentFontSize * 2; CTParagraphStyleSetting headIndentStyle; headIndentStyle.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent; headIndentStyle.valueSize = sizeof(headIndent); headIndentStyle.value = &headIndent; ? ? ? ? ? ?? 5.初始化文字對齊方式 ? ? ? ? ? ? CTTextAlignment alignment = kCTJustifiedTextAlignment; CTParagraphStyleSetting alignmentStyle; alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment; alignmentStyle.valueSize = sizeof(alignment); alignmentStyle.value = &alignment; ? ? ? ? ? ?? 6.初始化行間距 CGFloat lineSpace = 12.0f; CTParagraphStyleSetting lineSpaceStyle; lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacing; lineSpaceStyle.valueSize = sizeof(lineSpace); lineSpaceStyle.value = &lineSpace; ? ? ? ? ? ?? 7.初始化段間距 CGFloat paragraphSpace = 18; CTParagraphStyleSetting paragraphSpaceStyle; paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing; paragraphSpaceStyle.valueSize = sizeof(paragraphSpace); paragraphSpaceStyle.value = ¶graphSpace; ? ? ? ? ? ?? 8.將段落屬性設置到NSMutableAttributedString CTParagraphStyleSetting settings[4] = {headIndentStyle,alignmentStyle,lineSpaceStyle,paragraphSpaceStyle}; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate((const CTParagraphStyleSetting*)&settings,4); [attributeString addAttribute:(id)kCTParagraphStyleAttributeName? ? ? ? ? ? ? ? ? ? ? ? ? value:(id)paragraphStyle range:NSMakeRange(0, [attributeString length])]; CFRelease(paragraphStyle);
9.創建CTFramesetterRef ? ? ? ? ? ? CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributeString); ? ? ? ? ? ?? 10.繪制之前,翻轉繪圖坐標系 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); ? ? ? ? ? ?? 11.按照區域進行繪制 ? ? ?? CFIndex startIndex = 0;? NSInteger pathCount = 0; while (YES) { ? ?//構建繪圖區域 ? ?CGMutablePathRef columnPath = CGPathCreateMutable(); ? ?CGPathAddRect(columnPath, NULL, ? ? ? CGRectMake(20 + (pathCount%columnNum) * ((768-(columnNum+1)*20)/columnNum + 20), 50, (768-(columnNum+1)*20)/columnNum, 904)); ? ?//構建內容窗體 ? ?CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(startIndex,0), columnPath, NULL); ? ?//繪制內容窗體 ? ?CTFrameDraw(frame, context); ? ?//計算當前顯示結束位置的字符索引 ? ?CFRange currRange = CTFrameGetVisibleStringRange(frame); ? ?startIndex = startIndex + currRange.length; ? ?//釋放 ? ?CGPathRelease(columnPath); ? ?CFRelease(frame); ? ?//計數增加 ? ?? ? ?pathCount++; ? ?//結束 ? ?if (startIndex == [attributeString length]) { ? ? ? break; ? ?} }
12.按照行進行繪制 CFIndex start = 0; while (YES) { ? ?//判斷是否繪制完畢 ? ?if (start == attributeString.length) { ? ? ? break; ? ?} ? ?//根據內容、開始索引位置和繪制區域的寬度,返回推薦的換行位置索引 ? ?CFIndex count = CTTypesetterSuggestLineBreak(frameSetter, start, pageWidth); ? ?//創建一個新行 ? ?CTLineRef line = CTTypesetterCreateLine(frameSetter, CFRangeMake(start, count)); ? ?//獲取新行的排版屬性 ? ?? ? ?CGFloat ascent; ? ?CGFloat descent; ? ?CGFloat leading; ? ?CTLineGetTypographicBounds(line, &ascent, ?&descent, &leading); ? ?//計算新行的Y值 ? ? ? ? ? ? ? ?? ? ?imageY = imageY - lineSpace - ascent - descent - leading; ? ?//繪制行 ? ? ? ? ? ? ? ?? ? ?CGContextSetTextPosition(currContext, 0.0f, imageY); ? ?CTLineDraw(line, currContext); ? ?//釋放行對象 ? ? ? ? ? ? ? ?? ? ?CFRelease(line); ? ?//更改當前繪制的位置索引 ? ? ? ? ? ? ? ?? ? ?start += count; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
2.一個開源工具類-OHAttributedLabel,就是使用CoreText框架實現的,能夠實現一個Label中有不同的文字大小、文字顏色、字體以及鏈接等。 ?
二、一般使用步驟
1.創建NSMutableAttributedString NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:contentString]; ? ? ? ? ? ? ? ?? 2.設置文字顏色 ? ? ? ? ? ? ? ? [attributeString addAttribute:(id)kCTForegroundColorAttributeName ? ? ? ? ? ? ? ? ? ? ? ? value:(id)[UIColor darkGrayColor].CGColor? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, tempArticle.desc.length)];
2.設置字體以及大小 ? ? ? ? ? ? ? ? CTFontRef font = CTFontCreateWithName(CFSTR("Bodoni 72"), contentFontSize, NULL); [attributeString addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, [attributeString length])]; CFRelease(font);
4.初始化段落首行縮進樣式 CGFloat headIndent = contentFontSize * 2; CTParagraphStyleSetting headIndentStyle; headIndentStyle.spec = kCTParagraphStyleSpecifierFirstLineHeadIndent; headIndentStyle.valueSize = sizeof(headIndent); headIndentStyle.value = &headIndent; ? ? ? ? ? ?? 5.初始化文字對齊方式 ? ? ? ? ? ? CTTextAlignment alignment = kCTJustifiedTextAlignment; CTParagraphStyleSetting alignmentStyle; alignmentStyle.spec = kCTParagraphStyleSpecifierAlignment; alignmentStyle.valueSize = sizeof(alignment); alignmentStyle.value = &alignment; ? ? ? ? ? ?? 6.初始化行間距 CGFloat lineSpace = 12.0f; CTParagraphStyleSetting lineSpaceStyle; lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacing; lineSpaceStyle.valueSize = sizeof(lineSpace); lineSpaceStyle.value = &lineSpace; ? ? ? ? ? ?? 7.初始化段間距 CGFloat paragraphSpace = 18; CTParagraphStyleSetting paragraphSpaceStyle; paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing; paragraphSpaceStyle.valueSize = sizeof(paragraphSpace); paragraphSpaceStyle.value = ¶graphSpace; ? ? ? ? ? ?? 8.將段落屬性設置到NSMutableAttributedString CTParagraphStyleSetting settings[4] = {headIndentStyle,alignmentStyle,lineSpaceStyle,paragraphSpaceStyle}; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate((const CTParagraphStyleSetting*)&settings,4); [attributeString addAttribute:(id)kCTParagraphStyleAttributeName? ? ? ? ? ? ? ? ? ? ? ? ? value:(id)paragraphStyle range:NSMakeRange(0, [attributeString length])]; CFRelease(paragraphStyle);
9.創建CTFramesetterRef ? ? ? ? ? ? CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributeString); ? ? ? ? ? ?? 10.繪制之前,翻轉繪圖坐標系 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); ? ? ? ? ? ?? 11.按照區域進行繪制 ? ? ?? CFIndex startIndex = 0;? NSInteger pathCount = 0; while (YES) { ? ?//構建繪圖區域 ? ?CGMutablePathRef columnPath = CGPathCreateMutable(); ? ?CGPathAddRect(columnPath, NULL, ? ? ? CGRectMake(20 + (pathCount%columnNum) * ((768-(columnNum+1)*20)/columnNum + 20), 50, (768-(columnNum+1)*20)/columnNum, 904)); ? ?//構建內容窗體 ? ?CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(startIndex,0), columnPath, NULL); ? ?//繪制內容窗體 ? ?CTFrameDraw(frame, context); ? ?//計算當前顯示結束位置的字符索引 ? ?CFRange currRange = CTFrameGetVisibleStringRange(frame); ? ?startIndex = startIndex + currRange.length; ? ?//釋放 ? ?CGPathRelease(columnPath); ? ?CFRelease(frame); ? ?//計數增加 ? ?? ? ?pathCount++; ? ?//結束 ? ?if (startIndex == [attributeString length]) { ? ? ? break; ? ?} }
12.按照行進行繪制 CFIndex start = 0; while (YES) { ? ?//判斷是否繪制完畢 ? ?if (start == attributeString.length) { ? ? ? break; ? ?} ? ?//根據內容、開始索引位置和繪制區域的寬度,返回推薦的換行位置索引 ? ?CFIndex count = CTTypesetterSuggestLineBreak(frameSetter, start, pageWidth); ? ?//創建一個新行 ? ?CTLineRef line = CTTypesetterCreateLine(frameSetter, CFRangeMake(start, count)); ? ?//獲取新行的排版屬性 ? ?? ? ?CGFloat ascent; ? ?CGFloat descent; ? ?CGFloat leading; ? ?CTLineGetTypographicBounds(line, &ascent, ?&descent, &leading); ? ?//計算新行的Y值 ? ? ? ? ? ? ? ?? ? ?imageY = imageY - lineSpace - ascent - descent - leading; ? ?//繪制行 ? ? ? ? ? ? ? ?? ? ?CGContextSetTextPosition(currContext, 0.0f, imageY); ? ?CTLineDraw(line, currContext); ? ?//釋放行對象 ? ? ? ? ? ? ? ?? ? ?CFRelease(line); ? ?//更改當前繪制的位置索引 ? ? ? ? ? ? ? ?? ? ?start += count; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的CoreText使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring boot maven项目返
- 下一篇: RabbitMQ和Kafka选型用哪个