在iOS中对TextView的设置直接用就好了,但是在mac os开发中只设置textView是不可用的(因为无法滚动)。需要添加到scrollView上面。下面纯代码创建NSTextView.
//NSTextView
self.textView = [[NSTextView alloc] initWithFrame:NSMakeRect(20, 20, self.view.frame.size.width - 40, self.view.frame.size.height - 80)];
[self.view addSubview:self.textView];
self.textView.backgroundColor = [NSColor whiteColor];
self.textView.editable = NO;
self.textView.string = @"哈哈实力的水电费水电费水电费水电费水电费水电费水电费水电费说水电费水电费水电费是的加夫里什对方离开的哈哈实力的水电费水电费水电费水电费水电费水电费水电费水电费说水电费水电费水电费是的加夫里什对方离开的哈哈实力的水电费水电费水电费水电费水电费水电费水电费水电费说水电费水电费水电费是的加夫里什对方离开的哈哈实力的水电费水电费水电费水电费水电费水电费水电费水电费说水电费水电费水电费是的加夫里什对方离开的";
self.textView.textColor = [NSColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
//NSScrollView
self.scrollView = [[NSScrollView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, self.view.frame.size.height - 80)];
[self.scrollView setBorderType:NSNoBorder];
[self.scrollView setHasVerticalScroller:YES];
[self.scrollView setHasHorizontalScroller:NO];
[self.scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self.textView setMinSize:NSMakeSize(0.0, self.view.frame.size.height - 80)];
[self.textView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[self.textView setVerticallyResizable:YES];//垂直方向可以调整大小
[self.textView setHorizontallyResizable:NO];//水平方向不可以调整大小
[self.textView setAutoresizingMask:NSViewWidthSizable];
[[self.textView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[[self.textView textContainer] setWidthTracksTextView:YES];
[self.textView setFont:[NSFont fontWithName:@"PingFang-SC-Regular" size:18.0]];
[self.textView setEditable:NO];
[self.scrollView setDocumentView:self.textView];
[self.view addSubview:self.scrollView];
效果图如下: