国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CoreText使用介紹

2019-11-09 15:02:20
字體:
來源:轉載
供稿:網友
一、概述  1.CoreText是蘋果創建的一個用于文字排版的框架,可以實現文字排版、圖文混排等復雜的界面效果。從iOS3.2啟用。2.一個開源工具類-OHAttributedLabel,就是使用CoreText框架實現的,能夠實現一個Label中有不同的文字大小、文字顏色、字體以及鏈接等。  二、一般使用步驟1.創建NSMutableAttributedStringNSMutableAttributedString *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 = &paragraphSpace;            8.將段落屬性設置到NSMutableAttributedStringCTParagraphStyleSetting 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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹江口市| 定西市| 高雄县| 柞水县| 昌江| 彭泽县| 清镇市| 玉环县| 循化| 泗洪县| 龙陵县| 彰化市| 台南县| 马公市| 固原市| 磴口县| 平塘县| 辛集市| 丰台区| 房产| 保亭| 通城县| 中山市| 全南县| 金堂县| 皮山县| 永康市| 改则县| 营山县| 陈巴尔虎旗| 临海市| 诸城市| 临安市| 仁化县| 蒙自县| 南陵县| 宜川县| 霍林郭勒市| 长葛市| 威海市| 开远市|