2

私はCoreTextにかなり慣れていません。2つの属性付き文字列がありますが、違いは1つだけです。2番目の例では、最初の文字がVerdana-Italicsize22に設定されています。

最初のものは、CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedText);を通過するのに問題はありません。しかし、2番目のnoeはEXC_BAD_ACCESSでクラッシュします。

最初の文字にイタリックのverdanaを追加するだけで、2番目の文字がクラッシュする理由はありますか?

pCO{
    CTForegroundColor = "<CGColor 0x8664bb0> [<CGColorSpace 0x816eef0> (kCGColorSpaceDeviceGray)] ( 0 1 )";
    NSFont = "CTFont <name: Verdana, size: 22.000000, matrix: 0x0>\nCTFontDescriptor <attributes: <CFBasicHash 0x835a240 [0x13fab48]>{type = mutable dict, count = 1,\nentries =>\n\t1 : <CFString 0x278c40 [0x13fab48]>{contents = \"NSFontNameAttribute\"} = <CFString 0x86566e0 [0x13fab48]>{contents = \"Verdana\"}\n}\n>";
    NSParagraphStyle = "CTParagraphStyle:\nwriting direction = -1, alignment = 2, line break mode = 4, 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 0x8659590 [0x13fab48]>{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)}";
}2{
    NSSuperScript = "-1";
}

p{
    NSFont = "<UICFFont: 0x8484240> font-family: \"Verdana\"; font-weight: normal; font-style: italic; font-size: 22px";
}CO{
    CTForegroundColor = "<CGColor 0x847e820> [<CGColorSpace 0x8426810> (kCGColorSpaceDeviceGray)] ( 0 1 )";
    NSFont = "CTFont <name: Verdana, size: 22.000000, matrix: 0x0>\nCTFontDescriptor <attributes: <CFBasicHash 0x832ecb0 [0x13fab48]>{type = mutable dict, count = 1,\nentries =>\n\t1 : <CFString 0x278c40 [0x13fab48]>{contents = \"NSFontNameAttribute\"} = <CFString 0x847ccb0 [0x13fab48]>{contents = \"Verdana\"}\n}\n>";
    NSParagraphStyle = "CTParagraphStyle:\nwriting direction = -1, alignment = 2, line break mode = 4, 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 0x847ee20 [0x13fab48]>{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)}";
}2{
    NSSuperScript = "-1";
}

乾杯

ニック

4

2 に答える 2

7

ここでいくつかの回答を注意深く読んだ後、CTFontの代わりにUIFontを使用していることに気付きました。UIFontを次のようにCTFontに変換します。

CTFontRef CTFontCreateFromUIFont(UIFont *font)
{
    CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, 
                                            font.pointSize, 
                                            NULL);
    return ctFont;
}

次に、このようにNSAttributedStringに追加します

CTFontRef italicFont = CTFontCreateFromUIFont(italicUIFont);
[aStr addAttribute:(NSString*)kCTFontAttributeName value:(__bridge id)italicFont range:NSMakeRange(0,5)];

そして出来上がり、それはもうクラッシュせず、私が望んでいたように表示されます。:-)

乾杯

ニック

于 2012-05-30T19:20:33.343 に答える
1

Niklassaersの回答は、次のように誤って使用するとARCでリークする可能性があります。

楽器http://goo.gl/xrxwdb

したがって、属性を追加した後は、必ず次のことを行ってください。

CFRelease(italicFont);
于 2013-11-07T12:19:32.470 に答える