我有一个要显示水平滚动条的 NSTextView。按照互联网上的一些线索,我大部分都在工作,除了垂直滚动条有问题。
我所做的是找到最长线的宽度(以给定字体的像素为单位),然后适当地调整 NSTextContainer 和 NSTextView 的大小。这样,水平滚动条代表宽度,向右滚动将滚动到最长文本行的末尾。
完成这项工作后,我注意到我的 NSScrollView 会在我键入时显示和隐藏垂直滚动条。我通过在调整大小之前将 autohidesScrollers 设置为 NO 并在之后设置为 YES 来“解决”这个问题。但是,仍然存在另一个问题,当我键入时,垂直滚动条拇指会跳到滚动条的顶部,然后在我键入时返回到正确的位置。我输入'a' <space>,它跳到顶部,我再次按<space>,它跳回到正确的位置。
有什么想法吗?
这是一些示例代码:
- (CGFloat)longestLineOfText
{
CGFloat longestLineOfText = 0.0;
NSRange lineRange;
NSString* theScriptText = [myTextView string];
NSDictionary* attributesDict = [NSDictionary dictionaryWithObject:scriptFont forKey:NSFontAttributeName]; //scriptFont is a instance variable
NSUInteger characterIndex = 0;
NSUInteger stringLength = [theScriptText length];
while (characterIndex < stringLength) {
lineRange = [theScriptText lineRangeForRange:NSMakeRange(characterIndex, 0)];
NSSize lineSize = [[theScriptText substringWithRange:lineRange] sizeWithAttributes:attributesDict];
longestLineOfText = max(longestLineOfText, lineSize.width);
characterIndex = NSMaxRange(lineRange);
}
return longestLineOfText;
}
// ----------------------------------------------------------------------------
- (void)updateMyTextViewWidth
{
static CGFloat previousLongestLineOfText = 0.0;
CGFloat currentLongestLineOfText = [self longestLineOfText];
if (currentLongestLineOfText != previousLongestLineOfText) {
BOOL shouldStopBlinkingScrollBar = (previousLongestLineOfText < currentLongestLineOfText);
previousLongestLineOfText = currentLongestLineOfText;
NSTextContainer* container = [myTextView textContainer];
NSScrollView* scrollView = [myTextView enclosingScrollView];
if (shouldStopBlinkingScrollBar) {
[scrollView setAutohidesScrollers:NO];
}
CGFloat padding = [container lineFragmentPadding];
NSSize size = [container containerSize];
size.width = currentLongestLineOfText + padding * 2;
[container setContainerSize:size];
NSRect frame = [myTextView frame];
frame.size.width = currentLongestLineOfText + padding * 2;
[myTextView setFrame:frame];
if (shouldStopBlinkingScrollBar) {
[scrollView setAutohidesScrollers:YES];
}
}
}