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

首頁 > 系統 > iOS > 正文

IOS 中CATextLayer繪制文本字符串

2020-07-26 02:39:48
字體:
來源:轉載
供稿:網友

IOS 中CATextLayer繪制文本字符串

CATextLayer使用Core Text進行繪制,渲染速度比使用Web Kit的UILable快很多。而且UILable主要是管理內容,而CATextLayer則是繪制內容。

CATextLayer的繪制文本字符串的效果如下:

代碼示例:

// 繪制文本的圖層 CATextLayer *layerText = [[CATextLayer alloc] init]; // 背景顏色 layerText.backgroundColor = [UIColor orangeColor].CGColor; // 渲染分辨率-重要,否則顯示模糊 layerText.contentsScale = [UIScreen mainScreen].scale; // 顯示位置 layerText.bounds = CGRectMake(0.0, 0.0, 100.0, 50.0); layerText.position = position; // 添加到父圖書 [self.view.layer addSublayer:layerText]; 
// 分行顯示 layerText.wrapped = YES; // 超長顯示時,省略號位置 layerText.truncationMode = kCATruncationNone; // 字體顏色 layerText.foregroundColor = [UIColor purpleColor].CGColor; // 字體名稱、大小 UIFont *font = [UIFont systemFontOfSize:15.0]; CFStringRef fontName = (__bridge CFStringRef)font.fontName; CGFontRef fontRef =CGFontCreateWithFontName(fontName); layerText.font = fontRef; layerText.fontSize = font.pointSize; CGFontRelease(fontRef); // 字體對方方式 layerText.alignmentMode = kCAAlignmentJustified; 
// 字符顯示 NSString *text = @"devZhang is iOSDeveloper.devZhang is iOSDeveloper.devZhang is iOSDeveloper.devZhang is iOSDeveloper."; 
// 方法1-簡單顯示 layerText.string = text; 
// 方法2-富文本顯示 // 文本段落樣式 NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init]; textStyle.lineBreakMode = NSLineBreakByWordWrapping; // 結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") textStyle.alignment = NSTextAlignmentCenter; //(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然) textStyle.lineSpacing = 5; // 字體的行間距 textStyle.firstLineHeadIndent = 5.0; // 首行縮進 textStyle.headIndent = 0.0; // 整體縮進(首行除外) textStyle.tailIndent = 0.0; // textStyle.minimumLineHeight = 20.0; // 最低行高 textStyle.maximumLineHeight = 20.0; // 最大行高 textStyle.paragraphSpacing = 15; // 段與段之間的間距 textStyle.paragraphSpacingBefore = 22.0f; // 段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */ textStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 從左到右的書寫方向(一共➡️三種) textStyle.lineHeightMultiple = 15; /* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */ textStyle.hyphenationFactor = 1; //連字屬性 在iOS,唯一支持的值分別為0和1 // 文本屬性 NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] init]; // NSParagraphStyleAttributeName 段落樣式 [textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName]; // NSFontAttributeName 字體名稱和大小 [textAttributes setValue:[UIFont systemFontOfSize:12.0] forKey:NSFontAttributeName]; // NSForegroundColorAttributeNam 顏色 [textAttributes setValue:[UIColor greenColor] forKey:NSForegroundColorAttributeName]; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text]; [attributedText setAttributes:textAttributes range:NSMakeRange(0, 8)]; layerText.string = attributedText; 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白银市| 桂林市| 平舆县| 宜都市| 吉安市| 任丘市| 嘉兴市| 江华| 滕州市| 鸡西市| 连州市| 库车县| 华安县| 余姚市| 灵石县| 仙游县| 武邑县| 扎囊县| 丹巴县| 广德县| 永和县| 板桥市| 巴东县| 武川县| 彰武县| 荣昌县| 通化县| 靖江市| 民丰县| 鸡西市| 英超| 曲阳县| 巴林左旗| 河西区| 五原县| 合川市| 宝应县| 金乡县| 普陀区| 临安市| 桓台县|