IOS 에서 CATextLayer 텍스트 문자열 그리 기

3720 단어 IOSCATextLayer
IOS 에서 CATextLayer 텍스트 문자열 그리 기
CATextLayer 는 Core Text 를 사용 하여 그립 니 다.웹 키 트 를 사용 하 는 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; 

궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기