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

首頁 > 系統 > iOS > 正文

IOS 中CATextLayer繪制文本字符串

2019-10-21 18:44:18
字體:
來源:轉載
供稿:網友

IOS 中CATextLayer繪制文本字符串

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

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

CATextLayer,繪制文本字符串,IOS

代碼示例:

// 繪制文本的圖層 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; 

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


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎平县| 安平县| 天津市| 大田县| 湘西| 丁青县| 崇州市| 鲁山县| 石林| 临夏县| 乳山市| 论坛| 潮安县| 宁南县| 阿拉善左旗| 丽水市| 香港| 绩溪县| 扎兰屯市| 托克逊县| 息烽县| 文山县| 沙田区| 贡觉县| 铁岭市| 楚雄市| 青海省| 山阴县| 长海县| 玉门市| 河东区| 墨竹工卡县| 凭祥市| 邻水| 黑龙江省| 佳木斯市| 喀喇| 海安县| 习水县| 巴塘县| 安西县|