Wie füge ich Tabstopps zu einem NSAttributedString und die Anzeige in einem UITextView

Ich bin erstellen einer iOS-app, und ich würde gerne eine Anzeige zugeschrieben string mit
bestimmte tab-stops angegeben in einem UITextView. Ich würde auch gerne zeichnen
direkt in UIViews mit Core Text in drawRect. Ich möchte (wenn möglich)
für die gleichen zugeschrieben string verwendet werden, in beiden Szenarien.

So, in meiner app erstelle ich eine CFAttributedStringRef oder einen NSAttributedString und
anwenden einer CTParagraphStyle ihm zuschreiben. Dann versuche ich, zur Anzeige der
zugeschrieben string in einem UITextView und ich bekomme einen crash wie den folgenden:

-[__NSCFType headIndent]: unrecognized selector sent to instance 0x7545080

po 0x7545080
$0 = 122966144 CTParagraphStyle:
base writing direction = -1, alignment = 4, line break mode = 0, 
   default tab interval = 0
first line head indent = 0, head indent = 0, tail indent = 0
line height multiple = 0, maximum line height = 0, minimum line height = 0
line spacing adjustment = 0, paragraph spacing = 0, 
   paragraph spacing before = 0
tabs:
(
   "CTTextTab: location = 20, alignment = 0, options = (none)\n",
   "CTTextTab: location = 40, alignment = 0, options = (none)\n",
   "CTTextTab: location = 60, alignment = 0, options = (none)\n",
   "CTTextTab: location = 80, alignment = 0, options = (none)\n",
   "CTTextTab: location = 100, alignment = 0, options = (none)\n",
   "CTTextTab: location = 120, alignment = 0, options = (none)\n"
)

Verstehe ich, was Los ist, aber ich Frage mich, ob ich eine alternative Art und Weise
das zu tun, was ich möchte. NSMutableParagraphStyle auf iOS nicht über tab
halt Unterstützung, aber ich merke, dass NSMutableParagraphStyle auf OS X hat diese
- Fähigkeit. Dies führt mich zu glauben, dass die iOS-NSMutableParagraphStyle kann
eines Tages unterstützen.

In der Zwischenzeit ist es Weg, um Tabulatoren, um eine CFAttributedStringRef oder eine
NSAttributedString und haben noch einen UITextView ihn anzeigen?

Die Quelle in Frage zu stellen ist:

- (void)viewWillAppear:(BOOL)animated
{
   CFDictionaryRef attrs = (__bridge CFDictionaryRef) @{};
   CFAttributedStringRef a = CFAttributedStringCreate(
       kCFAllocatorDefault, CFSTR("a\tb\tc\td"), attrs);

   CFMutableAttributedStringRef attrStr;
   attrStr = CFAttributedStringCreateMutableCopy(
       kCFAllocatorDefault, CFAttributedStringGetLength(a), a);

   CFArrayRef tabStops = (__bridge CFArrayRef) @[
       (__bridge id) CTTextTabCreate(0, 20, NULL),
       (__bridge id) CTTextTabCreate(0, 40, NULL),
       (__bridge id) CTTextTabCreate(0, 60, NULL),
       (__bridge id) CTTextTabCreate(0, 80, NULL),
       (__bridge id) CTTextTabCreate(0, 100, NULL),
       (__bridge id) CTTextTabCreate(0, 120, NULL)];

   const CTParagraphStyleSetting paraSettings[] = {
       {kCTParagraphStyleSpecifierTabStops, sizeof(CFArrayRef), &tabStops},
   };

   CTParagraphStyleRef paraStyle = CTParagraphStyleCreate(
       paraSettings, 
       sizeof(paraSettings) / sizeof(CTParagraphStyleSetting));

   CFRange range = CFRangeMake(0, CFAttributedStringGetLength(attrStr));
   CFAttributedStringSetAttribute(
       attrStr, range, kCTParagraphStyleAttributeName, paraStyle);

   CFRelease(paraStyle);
   CFIndex i, count = CFArrayGetCount(tabStops);
   for (i = 0; i < count; i++) {
       CFRelease(CFArrayGetValueAtIndex(tabStops, i));
   }

   [[self textView] setAttributedText:
       (__bridge NSAttributedString *)(attrStr)];
}
Schreibe einen Kommentar