美文网首页
MacOS 开发(五):NSTextView纯代码实现

MacOS 开发(五):NSTextView纯代码实现

作者: SoaringHeart | 来源:发表于2020-04-02 14:14 被阅读0次

    因为系统机制导致,纯代码实现滚动视图的时候必须自己实现 scrollView,随封装如下:

    🌰🌰:
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self.view addSubview:self.textView.enclosingScrollView];
    }
    
    - (void)viewDidLayout{
        [super viewDidLayout];
    
        [self.textView.enclosingScrollView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.equalTo(self.view);
            make.width.equalTo(self.view).multipliedBy(0.5);
            make.bottom.equalTo(self.view).offset(0);
        }];
      //备注:textView.enclosingScrollView 和 textView.scrollView 是同一对象
    }
    
    #pragma mark -lazy
    -(UUTextView *)textView{
        if (!_textView) {
            _textView = ({
                UUTextView * view = [UUTextView create:CGRectZero];
                view.delegate = self;
                view.string = @"NSScrollView上无法滚动的NSTextView";
                view.font = [NSFont systemFontOfSize:12];
                
                view;
            });
        }
        return _textView;
    }
    

    视图封装:

    class UUTextView: NSTextView {
    
        lazy var scrollView: NSScrollView = {
            let scrollView = NSScrollView()
            scrollView.backgroundColor = NSColor.red
            scrollView.drawsBackground = false
            scrollView.hasHorizontalScroller = true
            scrollView.hasVerticalScroller = true
            scrollView.autohidesScrollers = true
            
            return scrollView
        }()
        
        var placeHolder: String?
        
        // MARK: -lifecycle
        override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
            super.init(frame: frameRect, textContainer: container)
            
            setupUI()
        }
        
        override init(frame frameRect: NSRect) {
            super.init(frame: frameRect)
            
            setupUI()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
    
            // Drawing code here.
            if string == "" && self != window?.firstResponder {
                guard let placeHolder = placeHolder else { return }
                let attDic = [NSAttributedString.Key.foregroundColor: NSColor.gray]
                let attString = NSAttributedString(string: placeHolder, attributes: attDic)
                attString.draw(at: NSMakePoint(4, 0))
            }
        }
        
        override func resignFirstResponder() -> Bool {
            needsDisplay = true
            return super.resignFirstResponder()
        }
        
        // MARK: -funtions
        func setupUI() {
            scrollView.documentView = self
        }
        
    }
    

    相关文章

      网友评论

          本文标题:MacOS 开发(五):NSTextView纯代码实现

          本文链接:https://www.haomeiwen.com/subject/ygrxphtx.html