2012-01-13 4 views
1

텍스트의 일부를 검색 결과로 강조 표시 할 수있는 사용자 지정 UITextView를 작성하려고합니다. 이렇게하려면 일반 텍스트 레이어 대신 자신의 CATextLayer를 배치하고 NSMutableAttributedString을 사용하여 텍스트의 강조 표시된 부분이있는 CATextLayer에 텍스트를 그립니다.CATextLayer가 CTParagraphStyleSetting 속성을 무시합니다.

문제는 텍스트의 Linespacing을 설정할 수 없다는 것입니다. NSMutableAttributedString의 font 및 fontcolor와 같은 속성을 을 통해 설정할 수 있습니다 [attrString setAttributes : attributes range : range]; 하지만 CTParagraphStyleSetting의 모든 특성은 무시됩니다.

- (void)redrawSelectableTextLayer 
{ 
attrString = [[NSMutableAttributedString alloc] initWithString:self.text]; 

//Write the attributes of the attributed String 
NSRange range = NSMakeRange(0, attrString.length); 
NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithCapacity:3]; 

[attributes setObject:(id)[UIColor redColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName]; 

CTFontRef aFont = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL); 
if(aFont){ 
    [attributes setObject:(id)aFont forKey:(NSString*)kCTFontAttributeName]; 
} 
CTParagraphStyleSetting settings[1]; 
CGFloat linespace = 140.0; 
CTLineBreakMode lbm = kCTLineBreakByCharWrapping; 
settings[0].spec = kCTParagraphStyleSpecifierLineBreakMode; 
settings[0].valueSize = sizeof(CTLineBreakMode); 
settings[0].value = &lbm; 
CTParagraphStyleRef style = CTParagraphStyleCreate(settings, 1); 
[attributes setObject:(id)style forKey:(NSString*)kCTParagraphStyleAttributeName]; 
CFRelease(style); 

[attrString setAttributes:attributes range:range]; 

//Set current Size of view 
CGFloat width = originalTextLayer.frame.size.width - 18.0; 
CGFloat height = [attrString boundingHeightForWidth:width]; 
selectedTextLayer.frame = CGRectMake(9.0f, 11.0f, width, height); 

//Draw the attributed String to the CATextLayer 
selectedTextLayer.string = attrString; 
} 

CATextLayer는 기본적으로 이러한 설정을 무시합니까? 아니면 내가 잘못하고있는 것이 있습니까?

답변

3

CATextLayer는 CAParagraphStaleSetting 속성을 지원하지 않습니다 (지원되는 속성의 전체 목록을 어디에서 찾을 수 있는지 알지 못합니다).

필자가 원하는대로 얻기 위해 사용자 정의 CALayer 구현을 작성했습니다.

관련 문제