iOS:CTFramesetterCreateWithAttributedString导致EXC_BAD_ACCESS,任何猜测为什么?

Kom*_*ave 0 string attributes exc-bad-access ios

我创建了一个从普通字符串生成属性字符串的方法.在attrString低于价值是它的内容非空和正确的.

在运行我的应用程序时,第二行失败了EXC_BAD_ACCESS.知道为什么吗?

__bridge_retained呼叫是iOS 5的要求.我也试过,__bridge但没有效果,问题仍然存在.

    NSMutableAttributedString *attrString = (NSMutableAttributedString*)[textLayer attributedString];
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFMutableAttributedStringRef)attrString);
Run Code Online (Sandbox Code Playgroud)

UPDATE

如果我做:

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

然后该NSLog行将无法编译; Xcode给出了警告错误说明"Incompatible pointer types passing 'char[3]' to parameter of type 'NSString *'".

所以我尝试了这个:

    NSLog([attrString description]);
Run Code Online (Sandbox Code Playgroud)

这给出了以下输出(我用<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)}"; }

rob*_*off 8

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

NSFont = "<CGFont (0x28f850): ArialMT>"
Run Code Online (Sandbox Code Playgroud)

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