1

考虑这段代码:

CGFloat largerLineSpacing = kStreamCellParagraphSpacing;

CTParagraphStyleSetting paragraphSettings[1] = {
    { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &largerLineSpacing }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, sizeof(*paragraphSettings));

此代码在 iPad 1 (5.1) 上运行时崩溃并出现 EXC_BAD_ACCESS,但在 5.1 模拟器或 iPad 3 (6.0) 上运行时不会。我的 C 很弱 - 我在 sizeof 上犯了一个愚蠢的错误吗?

4

2 回答 2

2

CTParagraphStyleCreate的文档建议它的第二个参数给出数组中的CTParagraphStyleSetting实例数paragraphSettings(在您的情况下为 1),而不是数组的字节大小。

如果您将代码更改为

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);

它应该工作。或者,如果您想应对将来添加更多设置,您可以尝试

int numElems = sizeof(paragraphSettings)/sizeof(paragraphSettings[0]);
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings,
                                                            numElems);
于 2012-10-29T16:28:54.003 回答
1

静态 CFIndex 常量 settingCount = 1;

CTParagraphStyleSetting paragraphSettings[settingCount] = {
    { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &largerLineSpacing }
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, settingCount);
于 2012-10-29T16:32:06.677 回答