iOS:CTFramesetterCreateWithAttributedString 导致 EXC_BAD_ACCESS,任何猜测原因?

标签 ios string attributes exc-bad-access

我创建了一个从普通字符串生成属性字符串的方法。 attrString以下值非空且内容正确。

运行我的应用程序后,第二行失败并显示 EXC_BAD_ACCESS .知道为什么吗?

__bridge_retained call 是 iOS 5 的要求 afaik。我也试过 __bridge但这没有效果,问题仍然存在。

    NSMutableAttributedString *attrString = (NSMutableAttributedString*)[textLayer attributedString];
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFMutableAttributedStringRef)attrString);

更新

如果我这样做:

    NSMutableAttributedString *attrString = (NSMutableAttributedString*)[textLayer attributedString];
    NSLog(@"%@", attrString);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFMutableAttributedStringRef)attrString);

然后 NSLog行不会编译; Xcode 发出警告 错误说明 "Incompatible pointer types passing 'char[3]' to parameter of type 'NSString *'" .

所以我尝试了这个:

    NSLog([attrString description]);

这会产生以下输出(出于隐私考虑,我已将字符串内容替换为 <snipped>):

2012-01-19 06:49:11.307 WritePath[2475:707] <Snipped> { CTForegroundColor = "<CGColor 0x281530> [<CGColorSpace 0x22d1a0> (kCGColorSpaceDeviceRGB)] ( 0 0 0 1 )"; NSFont = "<CGFont (0x28f850): ArialMT>"; NSParagraphStyle = "CTParagraphStyle:\nwriting direction = -1, alignment = 3, line break mode = 0, default tab interval = 0\nfirst line head indent = 0, head indent = 0, tail indent = 0\nline height multiple = 0, maximum line height = 0, minimum line height = 0\nline spacing adjustment = 0, paragraph spacing = 0, paragraph spacing before = 0\ntabs:\n<CFArray 0x287b50 [0x3f229630]>{type = immutable, count = 12, values = (\n\t0 : CTTextTab: location = 28, alignment = 0, options = (none)\n\n\t1 : CTTextTab: location = 56, alignment = 0, options = (none)\n\n\t2 : CTTextTab: location = 84, alignment = 0, options = (none)\n\n\t3 : CTTextTab: location = 112, alignment = 0, options = (none)\n\n\t4 : CTTextTab: location = 140, alignment = 0, options = (none)\n\n\t5 : CTTextTab: location = 168, alignment = 0, options = (none)\n\n\t6 : CTTextTab: location = 196, alignment = 0, options = (none)\n\n\t7 : CTTextTab: location = 224, alignment = 0, options = (none)\n\n\t8 : CTTextTab: location = 252, alignment = 0, options = (none)\n\n\t9 : CTTextTab: location = 280, alignment = 0, options = (none)\n\n\t10 : CTTextTab: location = 308, alignment = 0, options = (none)\n\n\t11 : CTTextTab: location = 336, alignment = 0, options = (none)\n\n)}"; }

最佳答案

我在您的属性字符串转储中注意到了这一点:

NSFont = "<CGFont (0x28f850): ArialMT>"

您是否使用 CGFontRef 作为 kCTFontAttributeName 键的值创建了属性字符串?您必须使用 CTFontRef,而不是 CGFontRef。它们是不同的。

关于iOS:CTFramesetterCreateWithAttributedString 导致 EXC_BAD_ACCESS,任何猜测原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918174/

相关文章:

ios - Swift 2 IOS 9 = clang : error: linker command failed with exit code 1 (use -v to see invocation)

python - 在给定特定索引的情况下,如何访问对象列表中的对象属性?

magento获取属性集中的可配置属性

Java:IndexOf(String string)返回错误字符

动态生成的 Perl Moose 访问器

ios - 如何在 swift 3 中获取类属性标签?

iphone - 无法理解此仅影响 iOS 4.3.x 用户的符号化崩溃报告背后的真正原因

ios - 在生产中完全禁用来自 XCGLogger 的日志

javascript - Json 字符串,在变量中注释掉引号

c# - 将字符串解析为 Unity3D 中的代码 (C#)