IOS 中CATextLayer繪制文本字符串
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中Weex 加載 .xcassets 中的圖片資源的實例詳解
這篇文章主要介紹了IOS中Weex 加載 .xcassets 中的圖片資源的實例詳解的相關(guān)資料,希望通過本文介紹能幫助到大家,實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08IOS開發(fā)教程之put上傳文件的服務(wù)器的配置及實例分享
IOS開發(fā)給人一種高大上的趕腳,弄的好像你不會IOS就不是個合格的程序員一樣,好吧,既然是大趨勢,那我們就來學(xué)習(xí)下吧,今天來看看put上傳文件的服務(wù)器配置及案例2014-06-06IOS 中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è)置的存儲方式,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12