【问题标题】:How can I add UIView to UITextView如何将 UIView 添加到 UITextView
【发布时间】:2021-05-07 14:22:50
【问题描述】:

我正在尝试在 UITextView 中附加一个 UIView。从视觉上来说应该是some text, banner, more text

这是我尝试过的:

// does nothing
text.textContainer.exclusionPaths = [UIBezierPath(rect: view.frame)]
// works but it also removes the text
text.addSubview(view)

这是我的课

class ViewController: BannerController {
    @IBOutlet weak var text: UITextView!
    
    override func viewDidLoad() {        
        let view = UIView()
        
        banners.append(view)
                        
        let myString = NSMutableAttributedString(string: "Text at the beginning\n")
                
        super.viewDidLoad()
        
        // text.textContainer.exclusionPaths = [UIBezierPath(rect: view.frame)]
        
        // text.addSubview(view)
        
        text.attributedText = myString
    }
}

我也看到了这个https://github.com/vlas-voloshin/SubviewAttachingTextView,但我不知道如何应用到我的代码中。

有可能吗?我是 Swift 的新手,我不知道该怎么做。

【问题讨论】:

  • 对于您提到的库“只需将您使用的UITextView 的类更改为SubviewAttachingTextView
  • text.textContainer.exclusionPaths = [UIBezierPath(rect: view.frame)],我猜你想使用view.bounds 而不是view.frame,但即使在这里,你的view 也有.zero。所以目前还不清楚你期望它如何工作。
  • @Larme,只是改成.bounds还是一样。 .zero 是什么意思?
  • 我的意思是CGRect.zerolet view = UIView(); print("view frame: \(view.frame),因此您将排除路径应用于“无”。

标签: swift


【解决方案1】:

【讨论】:

  • 我花了一些时间,但成功了。谢谢
猜你喜欢
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2023-03-21
  • 2014-05-16
相关资源
最近更新 更多