我正在尝试编写一个自定义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默认情况下会忽略这些设置,还是我做错了什么?
最佳答案
CATextLayer不支持CAParagraphStaleSetting属性(我不知道在哪里可以找到所支持属性的完整列表)。
现在,我已经编写了自定义CALayer实现,以获取所需的信息。
关于objective-c - CATextLayer忽略CTParagraphStyleSetting属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8852467/