7

iOSアプリを作成していますが、UITextViewで指定された特定のタブストップを持つ属性付き文字列を表示したいと思います。また、drawRectのCore Textを使用して、それらをUIViewに直接描画したいと思います。(可能であれば)両方のシナリオで同じ属性文字列を使用したいと思います。

そのため、私のアプリでは、CFAttributedStringRefまたはNSAttributedStringを作成し、それにCTParagraphStyle属性を適用します。次に、属性付きの文字列をUITextViewに表示しようとすると、次のようなクラッシュが発生します。

-[__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"
)

私は何が起こっているのか理解していますが、私がやりたいことをする別の方法があるかどうか疑問に思っています。iOSのNSMutableParagraphStyleにはタブストップのサポートがありませんが、OSXのNSMutableParagraphStyleにはこの機能があることに気付きました。これは、iOSNSMutableParagraphStyleがいつかこれをサポートするかもしれないと私に思い込ませます。

それまでの間、CFAttributedStringRefまたはNSAttributedStringにタブストップを追加し、それでもUITextViewに表示させる方法はありますか?

問題のソースは次のとおりです。

- (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)];
}
4

4 に答える 4

3

これは私のために働いたSwiftバージョンです:

    let tablInterval: CGFloat = 85.0
    let paragraphStyle = NSMutableParagraphStyle()
    let terms = NSTextTab.columnTerminatorsForLocale(NSLocale.currentLocale())
    let tabStop0 = NSTextTab(textAlignment: .Right, location: 0, options: [NSTabColumnTerminatorsAttributeName:terms])
    let tabStop1 = NSTextTab(textAlignment: .Right, location: tablInterval, options: [NSTabColumnTerminatorsAttributeName:terms])
    let tabStop2 = NSTextTab(textAlignment: .Right, location: tablInterval*2, options: [NSTabColumnTerminatorsAttributeName:terms])
    let tabStop3 = NSTextTab(textAlignment: .Right, location: tablInterval*3, options: [NSTabColumnTerminatorsAttributeName:terms])
    paragraphStyle.addTabStop(tabStop0)
    paragraphStyle.addTabStop(tabStop1)
    paragraphStyle.addTabStop(tabStop2)
    paragraphStyle.addTabStop(tabStop3)

    let attributedString = NSMutableAttributedString(string:text)
    attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:rangeAll)
于 2015-12-10T21:21:33.523 に答える