2011-12-19 14 views
5

です。NSTextView [1]でHTMLのサブセットを編集中です。<時>タグをシミュレートしたいと思います。NSTextAttachmentCellの高さは

NSTextAttachmentとカスタムNSTextAttachmentCellを使用する方法がわかりました。添付ファイルとセルを挿入するためのコードがすべて記述されています。問題は、セルの下に膨大な空きスペースがあることです。

このスペースはセル自体の一部ではありません。セルの赤い領域全体をペイントすると正確に正しいサイズになりますが、テキストビューは次のテキスト行を赤の非常に下に置きます。額はどれくらい多くのテキストがの上にあるかによって異なります。セル。残念ながら、<時>タグが重要な長い文書で作業しています。これは、アプリケーションに大きな問題を引き起こします。

何が起こっているのですか?

私の細胞サブクラスのお金パーツ:

- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer 
       proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position 
        characterIndex:(NSUInteger)charIndex { 
    lineFrag.size.width = textContainer.containerSize.width; 

    lineFrag.size.height = topMargin + TsStyleBaseFontSize * 
     heightFontSizeMultiplier + bottomMargin; 

    return lineFrag; 
} 

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
     characterIndex:(NSUInteger)charIndex 
     layoutManager:(NSLayoutManager *)layoutManager { 
    NSRect frame = cellFrame; 
    frame.size.height -= bottomMargin; 

    frame.size.height -= topMargin; 
    frame.origin.y += topMargin; 

    frame.size.width *= widthPercentage; 
    frame.origin.x += (cellFrame.size.width - frame.size.width)/2; 

    [color set]; 
    NSRectFill(frame); 
} 

[1]私はisEditableをセットし、それが生成マークアップのWebViewがunusablyダーティで特に、私は見つけることができませんでした試してみましたテキストをきちんと折り返す方法<p>タグ。あなたを変更してみてください

+ (NSAttributedString *)newHorizontalRuleAttributedStringWithStylebook:(TsStylebook*)stylebook { 
    TsHorizontalRuleCell * cell = [[TsHorizontalRuleCell alloc] initTextCell:@"—"]; 
    cell.widthPercentage = 0.33; 
    cell.heightFontSizeMultiplier = 0.25; 
    cell.topMargin = 12.0; 
    cell.bottomMargin = 12.0; 
    cell.color = [NSColor blackColor]; 

    NSTextAttachment * attachment = [[NSTextAttachment alloc] initWithFileWrapper:nil]; 
    attachment.attachmentCell = cell; 
    cell.attachment = attachment; 

    NSAttributedString * attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 

    NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@""]; 
    [str appendAttributedString:attachmentString]; 
    [str.mutableString appendString:@"\n"]; 

    return str; 
} 
+2

することができますあなたの 'NSTextView'にテキストの添付ファイルを挿入する方法を示すコードを投稿しますか? –

+0

Rob、私が見たいコードを追加しました。これを見てくれてありがとう! –

答えて

5

- (void)insertHorizontalRule:(id)sender { 
    NSAttributedString * rule = [TsPage newHorizontalRuleAttributedStringWithStylebook:self.book.stylebook]; 

    NSUInteger loc = self.textView.rangeForUserTextChange.location; 

    if(loc == NSNotFound) { 
     NSBeep(); 
     return; 
    } 

    if(loc > 0 && [self.textView.textStorage.string characterAtIndex:loc - 1] != '\n') { 
     NSMutableAttributedString * workspace = rule.mutableCopy; 
     [workspace.mutableString insertString:@"\n" atIndex:0]; 
     rule = workspace; 
    } 

    if([self.textView shouldChangeTextInRange:self.textView.rangeForUserTextChange replacementString:rule.string]) { 
     [self.textView.textStorage beginEditing]; 
     [self.textView.textStorage replaceCharactersInRange:self.textView.rangeForUserTextChange withAttributedString:rule]; 
     [self.textView.textStorage endEditing]; 
     [self.textView didChangeText]; 
    } 

    [self.textView scrollRangeToVisible:self.textView.rangeForUserTextChange]; 

    [self reloadPreview:sender]; 
} 

と添付ファイルの文字列を作成TsPageでの方法:水平ルールの添付ファイルを挿入するコードのためのロブKenigerの要求に答えるために


セルクラスのcellFrameForTextContainer:proposedLineFragment:glyphPosition:メソッドを呼び出すと、セルのフレームの起点としてNSZeroPointが返されます。

(私はこれが動作しなければならない理由はわかりませんが、質問者と私はライブのデバッグをしている、そしてそれが実際に行われます。)質問者によって追加されました


それがどのように見える、でも返される矩形は「フレーム」として記述されていますが、その起点はドキュメントの上端ではなく、その行に相対的です。したがって、戻り値のorigin.yの値は、同じ行にしたい場合は0に設定する必要があります。

(あなたは、セルの水平方向の位置を変更しない限り、それはlineFrag.origin.xと同じである必要がありますのでorigin.x値が、一方で、は、行のセルの位置を参照しない。)