考虑以下代码:
CGFloat largerLineSpacing = kStreamCellParagraphSpacing;
CTParagraphStyleSetting paragraphSettings[1] = {
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &largerLineSpacing }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, sizeof(*paragraphSettings));
在iPad 1(5.1)而不是5.1模拟器或iPad 3(6.0)上运行时,此代码会因EXC_BAD_ACCESS崩溃。我的C很弱-我在用sizeof做一个愚蠢的错误吗?
最佳答案
CTParagraphStyleCreate的文档建议其第二个参数给出CTParagraphStyleSetting
数组中paragraphSettings
实例的数量(在您的情况下为1),而不是数组的字节数。
如果您将代码更改为
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
它应该工作。或者,如果您希望以后再添加更多设置,可以尝试
int numElems = sizeof(paragraphSettings)/sizeof(paragraphSettings[0]);
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings,
numElems);