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

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

CoreText使用介紹

2019-11-09 14:06:36
字體:
來源:轉載
供稿:網友
一、概述  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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新河县| 萨嘎县| 鄂伦春自治旗| 崇仁县| 南城县| 通州区| 建昌县| 错那县| 广昌县| 青岛市| 陕西省| 吴堡县| 叶城县| 香河县| 木兰县| 林甸县| 高尔夫| 连南| 张掖市| 永顺县| 高青县| 安阳市| 正镶白旗| 丰宁| 义乌市| 菏泽市| 天祝| 衡东县| 来凤县| 淳化县| 兴义市| 丘北县| 清水县| 普宁市| 宜良县| 手游| 泰来县| 乐清市| 怀集县| 库伦旗| 崇义县|