gpt4 book ai didi

ios - 具有多个 NSTextContainer 的 NSLayoutManager 导致 UITextViews 不可选择/不可编辑

转载 作者:可可西里 更新时间:2023-10-31 23:44:32 25 4
gpt4 key购买 nike

我正在尝试实现多页文本编辑布局,如 Pages、MS Word 等。在 OS X 上,我可以通过为多个 NSTextView 创建一个 NSLayoutManager 和一个 NSTextStorage 来实现这一点。每个 NSTextView 都有自己的 NSTextContainer。请参阅下面的 OS X 代码。一个简单的示例,文本在两个 NSTextView 之间传播:

import Cocoa

class ViewController: NSViewController {

let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage(attributedString: NSAttributedString(string: "This is a test"))
let textContainer1 = NSTextContainer()
let textContainer2 = NSTextContainer()

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

textStorage.addLayoutManager(layoutManager)

textContainer1.widthTracksTextView = true
textContainer1.heightTracksTextView = true
textContainer2.widthTracksTextView = true
textContainer2.heightTracksTextView = true

let textView1 = NSTextView(frame: CGRectMake(0, 100, 100, 100), textContainer: textContainer1)
textView1.backgroundColor = NSColor.greenColor()
self.view.addSubview(textView1)
layoutManager.addTextContainer(textContainer1)


let textView2 = NSTextView(frame: CGRectMake(200, 100, 100, 100), textContainer: textContainer2)
textView2.backgroundColor = NSColor.greenColor()
self.view.addSubview(textView2)
layoutManager.addTextContainer(textContainer2)
}
}

这有效。

但是,当我尝试在 iOS 上使用 UITextView 执行相同操作时,UITextView 变得不可选择或不可编辑。查看我的 iOS 代码:

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage(attributedString: NSAttributedString(string: "This is a test. This text should spread over two UITextViews. Bla bla bla bla bla bla bla bla bla bla bla bla bla"))
let textContainer1 = NSTextContainer()
let textContainer2 = NSTextContainer()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

textStorage.addLayoutManager(layoutManager)

textContainer1.heightTracksTextView = true
textContainer1.widthTracksTextView = true
textContainer2.heightTracksTextView = true
textContainer2.widthTracksTextView = true

let textView1 = UITextView(frame: CGRectMake(0, 100, 100, 100), textContainer: textContainer1)
textView1.scrollEnabled = false
textView1.delegate = self
textView1.editable = true
textView1.selectable = true
textView1.backgroundColor = UIColor.greenColor()
self.view.addSubview(textView1)
layoutManager.addTextContainer(textContainer1)

let textView2 = UITextView(frame: CGRectMake(200, 100, 100, 100), textContainer: textContainer2)
textView2.scrollEnabled = false
textView2.delegate = self
textView2.editable = true
textView2.selectable = true
textView2.backgroundColor = UIColor.greenColor()
self.view.addSubview(textView2)
layoutManager.addTextContainer(textContainer2)

}
}

文本从一个 UITextView 流向另一个,但不可编辑。我将非常感谢任何建议。我用谷歌搜索了这个问题,发现其他人遇到了同样的问题,但没有找到解决方案。

最佳答案

我也一直在寻找答案,似乎当 UITextView 与其他 TextView 共享布局管理器时,不幸的是 TextView 变为静态。

iOS 7 Programming Pushing the Limits: Develop Advance Applications for Apple,第 375 页中概述了此问题:

Because you can easily add additional text containers to a layout manager, and because UITextView uses a layout manager, you may think it would be easy to create a multicolumn, editable UITextView. Unfortunately, this is not the case. If a UITextView is assigned multiple text containers [sic], it becomes static and cannot respond to user interaction such as editing or selection. This is a known issue and is currently "as designed".

关于ios - 具有多个 NSTextContainer 的 NSLayoutManager 导致 UITextViews 不可选择/不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com