1
我試圖編寫一個自定義的UITextView,可以將部分文本作爲搜索結果進行高亮顯示。爲此,我將自己的CATextLayer放置在普通文本層的位置,並希望使用NSMutableAttributedString在文本的高亮部分繪製CATextLayer上的文本。CATextLayer忽略CTParagraphStyleSetting屬性
問題是,我無法爲文本設置Linespacing。我可以通過 [attrString setAttributes:attributes range:range];將屬性設置爲類似於NSMutableAttributedString的字體和fontcolor; 但是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默認情況下是否忽略這些設置,或者有什麼我做錯了嗎?