IOS 에서 CATextLayer 텍스트 문자열 그리 기
3720 단어 IOSCATextLayer
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;
궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
IOS에서 ReplayKit 및 RTC 사용 방법응용된 소리와 아나운서의 소리를 포함한다.이 두 가지 수요를 감안하여 우리는 스크린 공유를 하는 생방송에 필요한 미디어 흐름을 간단하게 분석할 수 있다. 만약 우리가 Audio App과 Audio Mic를 두 개의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.