[英]Appending to NSTextView
我在后台運行了一個NSTask
(有一個NSPipe
設置),我想在NSTextView
( output
)中輸出內容,因為它們正在進入。
我正在使用的代碼是:
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithString:s];
//[str addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0, [str length])];
[[output textStorage] appendAttributedString:str];
[output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];
問題:
NSTextView
位於工作表上,當鼠標指針位於其他位置而不是懸停在NSTextView
上方時,似乎沒有顯示NSTextView
NSTextView
的顏色/插入顏色 /等,但這似乎不適用於新插入的文本? NSTextView
上添加(+滾動)的建議方法是什么? 謝謝!
請記住,用戶界面元素,這包括NSTextView
,在主線程上做他們的魔術。 如果您嘗試向文本視圖添加信息,那么您最好這樣做。 這是如何做:
[[output textStorage] performSelectorOnMainThread:@selector(appendAttributedString:)
withObject:str
waitUntilDone:YES];
我會談到你的第三點,但坦率地說,這是我仍然非常學生的事情。
為了解決你的第四點,看起來你已經弄清楚了; 只需合並你的追加和滾動動作。 但就像更改textStorage
的內容textStorage
,您希望確保在主線程上執行此操作。 由於-scrollRangeToVisible:
不為其參數提供對象,因此必須以不同的方式執行此操作:
dispatch_async(dispatch_get_main_queue(), ^{
[output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];
});
盡管我的第一個例子是,你可以將你的調用-appendAttributedString:
在那個塊里面:
dispatch_async(dispatch_get_main_queue(), ^{
[[output textStorage] appendAttributedString:str];
[output scrollRangeToVisible:NSMakeRange([[output string] length], 0)];
});
關於推薦的附加NSTextView的方法:你在appendAttributedString:中表現得很好,但是建議在shouldChangeTextInRange中保護它,然后是beginEditing,appendAttributedString,最后是endEditing:
textStorage = [textView textStorage];
if([textView shouldChangeTextInRange:range replacementString:string])
{
[textStorage beginEditing];
[textStorage replaceCharactersInRange:range withAttributedString:attrStr];
// or if you've already set up the attributes (see below)...
// [textStorage replaceCharactersInRange:range withString:str];
[textStorage endEditing];
}
我強烈建議替換scrollRangeToVisible:通過scrollToPoint :,作為scrollRangeToVisible:會導致大量閃爍,當你向下移動“范圍”時它也會逐漸變慢。
一種快速而骯臟的方式可能是這樣的:
- (void)scrollToBottom
{
NSPoint pt;
id scrollView;
id clipView;
pt.x = 0;
pt.y = 100000000000.0;
scrollView = [self enclosingScrollView];
clipView = [scrollView contentView];
pt = [clipView constrainScrollPoint:pt];
[clipView scrollToPoint:pt];
[scrollView reflectScrolledClipView:clipView];
}
我讓constrainScrollPoint做所有的計算工作。 我這樣做,因為無論如何我的計算失敗了(Apple和其他人使用visRect / docRect坐標建議的那些,產生了不可靠的結果)。 reflectScrolledClipView也很重要; 它會更新滾動條,使其具有正確的比例和位置。
您可能還會發現滾動發生時很有趣。 如果是這樣,請同時訂閱NSViewBoundsDidChangeNotification和NSViewFrameDidChangeNotification。 當其中一個出現時,滾動條位置很可能發生變化(調查[textView visibleRect]和[textView bounds])。
我看到你也有文本屬性的問題。 我也是這么久了。 我發現附加一個屬性字符串會有很多幫助,但對於輸入的文本來說仍然不夠。 ..然后我發現了關於打字的屬性。 在設置NSTextView時,例如在-awakeFromNib中,您可以從以下內容中選擇您喜歡的內容...
NSMutableParagraphStyle *paragraphStyle;
float characterWidth;
NSFont *font;
uint32_t tabWidth;
NSMutableDictionary *typingAttributes;
tabWidth = 4;
font = [NSFont fontWithName:@"Monaco" size:9.0];
paragraphStyle = [[textView defaultParagraphStyle] mutableCopy];
if(NULL == paragraphStyle)
{
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
// or maybe:
// paragraphStyle = [NSParagraphStyle new];
}
characterWidth = [[font screenFontWithRenderingMode:NSFontDefaultRenderingMode] advancementForGlyph:(NSGlyph)' '].width;
[paragraphStyle setDefaultTabInterval:(characterWidth * (float) tabWidth];
[paragraphStyle setTabStops:[NSArray array]];
typingAttributes = [[textView typingAttributes] mutableCopy];
if(NULL == typingAttributes)
{
typingAttributes = [NSMutableDictionary new];
}
[typingAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[typingAttributes setObject:font forKey:NSFontAttributeName];
[textView setTypingAttributes:attributes];
...這比您可能需要的方式更多,但它顯示了如何設置字體,標簽寬度和輸入屬性。 NSForegroundColorAttributeName也可能對您有意義(以及一些其他屬性,在Xcode中鍵入NSForegroundColorAttributeName並選項 - 雙擊它,然后您將看到更多屬性(您也可以命令雙擊;這將帶您到頭文件中的定義)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.