【问题标题】:iOS: CTFramesetterCreateWithAttributedString causes EXC_BAD_ACCESS, any guesses why?iOS:CTFramesetterCreateWithAttributedString 导致 EXC_BAD_ACCESS,任何猜测为什么?
【发布时间】:2012-01-18 22:05:20
【问题描述】:

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

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

__bridge_retained 调用是 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)}"; }

【问题讨论】:

  • 您应该在此处使用__bridge 以避免内存泄漏。如果你NSLog("%@", attrString),输出是什么?将输出粘贴到您的问题中。
  • @rob 感谢您的回复 - 我已经按照您的要求完成了,见上文。
  • 对不起,我应该说NSLog(@"%@", attrString)。但是你做的没问题。
  • 您是否尝试过使用更简单的属性字符串?例如。 NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"hello"]?或者 Core Foundation 版本:CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, CFSTR("hello"), NULL).

标签: ios string attributes exc-bad-access


【解决方案1】:

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

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

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

【讨论】:

  • 我现在在工作,但会在大约 8 小时后检查我何时回家并再次回复。非常感谢您的建议,并且发现它做得很好,我敢打赌你是对的 :-) 我知道它们是不同的,但我在我定义的 CATextLayer 子类中有一个自动“attributeString”方法,它假设'font' 属性中的 CTFont,我敢打赌,我无意中在构造函数中设置了 CGFont(该属性都可以接受)。
  • 你说得对 Rob,该方法现在完美运行 :-) 非常感谢您抽出时间回复,如果我发现我的错误需要更长的时间。竖起大拇指!
  • 遇到了这个确切的问题,发现我已经将 UIFont 和 UIColor 对象都放入了。所以感谢这解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
相关资源
最近更新 更多