2014-01-22 49 views
2

我希望用戶能夠滾動NSTextView,以便文本的最後一行位於視圖的頂部。默認情況下,具有多頁文本的NSTextView只允許用戶滾動,直到文本的最後一行位於視圖的底部。我怎樣才能做到這一點?如何允許滾動NSTextView的結尾?

回答

3

查看子類化NSLayoutManager並將您的子類分配給有問題的NSTextView。它應該是一個非常簡單的方法來檢測什麼時候被要求佈置最後一行文本,並返回一個更高的矩形。你甚至可以使用:

- (void)setExtraLineFragmentRect:(NSRect)fragmentRect usedRect:(NSRect)usedRect textContainer:(NSTextContainer *)container; 

// Sets the bounds and container for the extra line fragment. The extra line fragment is used when the text backing ends with a hard line break or when the text backing is totally empty, to define the extra line which needs to be displayed at the end of the text. If the text backing is not empty and does not end with a hard line break, this should be set to NSZeroRect and nil. Line fragment rects and line fragment used rects are always in container coordinates. 

你也可以嘗試子類化NSTextView的大小調整方法,並在其中添加空間。比如,嘗試子類化-setFrameSize:並查看添加的內容(scrollView的內容區域的高度) - (最後一行文本的高度),並調用super。

+0

謝謝,我會嘗試這兩個想法。 – alltom

+0

增加'fragmentRect'的大小不會導致滾動區域被擴展。增加'usedRect'的作用,但它也會導致遊標被畫得很大。 :) – alltom

+0

好吧,我更新了我的答案,接下來我會嘗試。 –