2012-01-13 15 views
1

私は、検索結果としてテキストの一部を強調表示するカスタムUITextViewを作成しようとしています。これを行うには、通常のテキストレイヤの代わりに独自のCATextLayerを配置し、NSMutableAttributedStringを使用して、テキストのハイライト部分を含むCATextLayerにテキストを描画します。CATextLayerはCTParagraphStyleSetting属性を無視しています

問題は、私はテキストのLinespacingを設定できないということです。私は [attrString setAttributes:attributes range:range]を介して、NSMutableAttributedStringのようなフォントとフォントに属性を設定できます。 ただし、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実装を書いて、自分が望むものを手に入れました。

関連する問題