首页 > 解决方案 > 在拖放之间丢失 NSAttributedString 的属性

问题描述

在 UICollectionView 和另一个 UIView 之间拖动,我得到 NSAttributedString 但没有 .font 属性。

掉落功能:

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
    session.loadObjects(ofClass: NSAttributedString.self, completion: { providers in
        let dropPoint = session.location(in: self)
        for attributedString in providers as? [NSAttributedString] ?? [] {
            \\ gets here
            for attr in attributedString.attributes(at: 0, effectiveRange: nil) {

                print(attr.key, attr.value) // no attributes to print

            }
        }
    })
}

和拖动功能:

func dragItemFor(indexPath: IndexPath) -> [UIDragItem]{
    if let cell = eiCollectionView.cellForItem(at: indexPath) as? EiCollactionViewCell {
        if let attributedString = cell.label.attributedText {
            for attr in attributedString.attributes(at: 0, effectiveRange: nil) {
                print("pre drag vc: ",attr.key, attr.value)// prints the font 
            }
            let dragItem =  UIDragItem(itemProvider: NSItemProvider(object:  attributedString))
            dragItem.localObject = attributedString
            return [dragItem]
        }
    }
    return []
}

标签: swiftuicollectionview

解决方案


问题是您的属性字符串没有开始的属性。.SFUIDisplay不是字体值。这只是说“默认”的一种方式。如果你想要一个字体,你需要设置一个字体。


推荐阅读