每次属性字符串超过特定高度时都尝试创建新的 NSTextView?

问题描述

每当属性字符串超出特定范围时,我都会尝试将新的 NSTextView 添加到我的集合视图中的最后一个索引。代码运行完美,直到第 5 个项目然后它开始它每次按下 Enter 按钮时开始创建一个项目。我认为这是一个错误,但我不确定。如果有人可以向我展示更好的方法或改进我拥有的当前代码,我将不胜感激。下面是我的代码:

这是 CollectionViewItem

class DocumentItem: NSCollectionViewItem {

   var itemView: DocumentTextView?
   
   
   
   override func viewDidLoad() {
       super.viewDidLoad()
       self.itemView?.wantsLayer = true
       
       
   
       // Do view setup here.
   }

   override func loadView() {
       self.itemView = DocumentTextView(frame: NSZeroRect)
       self.view = self.itemView!
       
   }

   func getView() -> DocumentTextView {
       return self.itemView!
   }
   
   
   
}


这里是 collectionView 数据源

func collectionView(_ collectionView: NSCollectionView,numberOfItemsInSection section: Int) -> Int {
            return DocList.count
        }
            
        
func collectionView(_ collectionView: NSCollectionView,itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
       
            let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue:  "DocumentItem"),for: indexPath)
            
            return item
}

这里是 NSTextView 子类

class DocumentTextView: NSTextView {
    
    
    var theContainer = NSTextContainer()
    var theStorage = NSTextStorage()
    var theManager = NSLayoutManager()
    
    
    var table = NSTextTable()
    var pdfPage: PDFPage?
    
    


   

    override init(frame frameRect: NSRect) {
        super.init(frame: NSRect(origin: frameRect.origin,size: NSSize(width: 800,height: 1131 )),textContainer: theContainer)
        theStorage.addLayoutManager(theManager)
        theManager.addTextContainer(theContainer)
        self.textContainerInset = CGSize(width: 50,height: 50)
        self.textContainer?.widthTracksTextView = true
        self.textContainer?.heightTracksTextView = true
        self.textContainer?.lineBreakMode = .byWordWrapping
        self.maxSize = NSSize(width: 800,height: 1131)
        self.backgroundColor = NSColor.fromHexString("ffffff")!
        self.isRichText = true
       

    
        
    }
  


   
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
}

这是带来bug的函数


  func textDidChange(_ notification: Notification) {
        var textView = notification.object as? DocumentTextView
        let numberOfItems = theDocumentOutlineView.numberOfItems(inSection: 0)
        let theLastTextView = theDocumentOutlineView.item(at: numberOfItems - 1) as! DocumentItem
        if textView == theLastTextView.itemView {
            print(textView?.attributedString().size())
            if (textView?.attributedString().size().height)! >= 1106.0 {
                self.DocList.append(2)
                var set = Set<IndexPath>()
                set.insert(NSIndexPath(forItem: self.DocList.count - 1,inSection: 0) as IndexPath)
                theDocumentOutlineView.insertItems(at: set)
                theDocumentOutlineView.scrollToItems(at: set,scrollPosition: NSCollectionView.ScrollPosition.top)
                var newFirstResponder = theDocumentOutlineView.item(at: self.DocList.count - 1) as! DocumentItem
                newFirstResponder.itemView?.delegate = self
                self.view.window?.makeFirstResponder(newFirstResponder.itemView)
                
            
            }
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)