我想要一些带有自定义行距的文本,所以我写了一个属性字符串CTParagraphStyleAttributte
并将其传递给我的CATextLayer
:
UIFont *font = [UIFont systemFontOfSize:20];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef)font.fontName,
font.pointSize, NULL);
CGColorRef cgColor = [UIColor whiteColor].CGColor;
CGFloat leading = 25.0;
CTTextAlignment alignment = kCTRightTextAlignment; // just for test purposes
const CTParagraphStyleSetting styleSettings[] = {
{kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof(CGFloat), &leading},
{kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment}
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(styleSettings, 2));
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)ctFont, (id)kCTFontAttributeName,
(id)cgColor, (id)kCTForegroundColorAttributeName,
(id)paragraphStyle, (id)kCTParagraphStyleAttributeName,
nil];
CFRelease(ctFont);
CFRelease(paragraphStyle);
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
initWithString:string
attributes:attributes];
_textLayer.string = attrStr;
[attrStr release];
但是行高没有改变。我想我在这里遗漏了一些东西,但我不知道是什么。
我已经尝试过kCTParagraphStyleSpecifierLineSpacingAdjustment
,kCTParagraphStyleSpecifierLineSpacing
但它们中的任何一个似乎都不起作用(?)。我还尝试使用kCTParagraphStyleSpecifierAlignment
(我知道CATextLayer
有一个属性)来设置对齐方式,只是为了测试kCTParagraphStyleAttributeName
确实有效,但它没有。
我注意到即使我传递了一些疯狂的值(例如:)CTParagraphStyleCreate(styleSettings, -555);
,这也会让我问自己:是否CATextLayer
支持段落属性?如果是这样,我在这里错过了什么?