CATextLayer忽略了CTParagraphStyleSetting属性

时间:2012-01-13 14:58:46

标签: objective-c ios

我正在尝试编写一个自定义UITextView,能够突出显示部分文本作为搜索结果。要做到这一点,我将自己的CATextLayer放在普通文本层的位置,并希望使用NSMutableAttributedString在CATextLayer上绘制带有突出显示部分文本的文本。

问题是,我无法为文本设置Linespacing。我可以通过将属性设置为NSMutableAttributedString的类似字体和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是否忽略这些设置,或者我做错了什么?

1 个答案:

答案 0 :(得分:3)

CATextLayer不支持CAParagraphStaleSetting属性(我不知道在哪里可以找到支持的属性的完整列表)。

我现在已经编写了自定义CALayer实现来获得我想要的内容。