欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS 中CATextLayer繪制文本字符串

 更新時間:2017年10月11日 10:36:55   作者:番薯大佬  
這篇文章主要介紹了IOS 中CATextLayer繪制文本字符串的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下

IOS 中CATextLayer繪制文本字符串

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

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; // 結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") 
textStyle.alignment = NSTextAlignmentCenter; //(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然) 
textStyle.lineSpacing = 5; // 字體的行間距 
textStyle.firstLineHeadIndent = 5.0; // 首行縮進(jìn) 
textStyle.headIndent = 0.0; // 整體縮進(jìn)(首行除外) 
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; 

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

相關(guān)文章

  • iOS 圖片裁剪 + 旋轉(zhuǎn)

    iOS 圖片裁剪 + 旋轉(zhuǎn)

    之前分別介紹了圖片裁剪和圖片旋轉(zhuǎn)的方法,裁剪和旋轉(zhuǎn)是可以連在一起執(zhí)行的。本文將對此進(jìn)行介紹,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • 如何利用iCloud Drive同步Xcode配置詳解

    如何利用iCloud Drive同步Xcode配置詳解

    這篇文章主要給大家介紹了關(guān)于如何利用iCloud Drive同步Xcode配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS實現(xiàn)輪播圖banner示例

    iOS實現(xiàn)輪播圖banner示例

    本篇文章主要介紹了iOS實現(xiàn)輪播圖banner示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • iOS實現(xiàn)代碼只執(zhí)行一次

    iOS實現(xiàn)代碼只執(zhí)行一次

    本文給大家分享的是在iOS中控制代碼在整個軟件生命周期中只運行一次的代碼,有需要的小伙伴可以參考下。
    2016-03-03
  • IOS圖片無限輪播器的實現(xiàn)原理

    IOS圖片無限輪播器的實現(xiàn)原理

    這篇文章主要介紹了IOS圖片無限輪播器的實現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • IOS中Weex 加載 .xcassets 中的圖片資源的實例詳解

    IOS中Weex 加載 .xcassets 中的圖片資源的實例詳解

    這篇文章主要介紹了IOS中Weex 加載 .xcassets 中的圖片資源的實例詳解的相關(guān)資料,希望通過本文介紹能幫助到大家,實現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08
  • iOS10適配(必看)

    iOS10適配(必看)

    這篇文章主要介紹了iOS10適配(必看)的相關(guān)資料,本文圖文并茂介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • IOS開發(fā)教程之put上傳文件的服務(wù)器的配置及實例分享

    IOS開發(fā)教程之put上傳文件的服務(wù)器的配置及實例分享

    IOS開發(fā)給人一種高大上的趕腳,弄的好像你不會IOS就不是個合格的程序員一樣,好吧,既然是大趨勢,那我們就來學(xué)習(xí)下吧,今天來看看put上傳文件的服務(wù)器配置及案例
    2014-06-06
  • IOS 中l(wèi)oadView,viewDidLoad,viewDidUnload詳解及使用

    IOS 中l(wèi)oadView,viewDidLoad,viewDidUnload詳解及使用

    這篇文章主要介紹了IOS 中l(wèi)oadView,viewDidLoad,viewDidUnload詳解及使用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 詳解iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲方式

    詳解iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲方式

    這篇文章主要介紹了iOS開發(fā)中app的歸檔以及偏好設(shè)置的存儲方式,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12

最新評論