- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试实现多页文本编辑布局,如 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/
我正在尝试使用以下方法在给定恒定宽度的情况下测量 NSAttributedString 的高度: -(CGFloat)calculateHeightForAttributedString:(NSAtt
我正在尝试实现 exclusionPaths对于我在里面的自定义 View UITextView ,并在类似于 Pages 应用程序的文本编辑过程中将它们与文本一起移动。 我正在尝试为 attribu
我有一个包含 NSString 内容的段落的 NSOrderedSet。遍历所有,创建一个大字符串并赋予 NSTextStorage。但是所有的段落都丢失了。 下面的代码允许我计算页面数并在页面中显示
我如何制作一个 NSLayoutManager 将其文本中的每个段落分配给不同的 NSTextContainer? 这类似于 TextKit 中常见的多页/多列用法,但每个页面对应一个可变长度的不同段
您好,我正在使用适用于 iOS7 的新 TextKit API,我正在尝试生成一个具有不规则形状的 UITextView。到目前为止,我在 View Controller 中有: -(void) lo
我正在尝试使用以下代码获取在给定文本容器中显示文本所需的矩形大小: s = [manager usedRectForTextContainer:container].size; 我希望文本是多行的,但
我发现 NSTextView 中文本容器的行为非常有趣。当我设置容器的大小使其小于 NSTextView 框架的大小并尝试在 NSTextView drawRect: 中绘制任何图形(例如线条、矩形)
我正在创建一系列 NSTextContainer 来保存来自 HTML 资源的文本。我能够将 HTML 添加到属性字符串,将其分配给 NSTextStorage 和 NSLayoutManager,并
抱歉,如果问题看起来很微不足道: 我正在尝试使用其底层 setter 设置 NSTextContainer 实例属性 exclusionPaths:[myNSTCInstance setExclusi
我有一个 NSTextView 的子类,在这个子类的初始化器中我想调用: super.init(frame: NSMakeRect(...)) 这是我在以编程方式初始化 NSTextView 时经常做
我将 Text Kit 与 UIPageViewController 结合使用来创建一本书。原始文本存储在文本文件 (.html) 中并放入 NSTextStorage。 [NSLayoutManag
代码示例 我有一个 NSLayoutManager、NSTextContainer 和 NSTextStorage 作为自定义 NSView(不是 TextView)中的属性,在 awakeFromN
按照苹果文档,我试图通过它的两个构造函数方法设置一个简单的 NSTextView。 我将下面的代码放在内容 View 的 View Controller 的 viewDidAppear 方法中。 te
我正在尝试实现多页文本编辑布局,如 Pages、MS Word 等。在 OS X 上,我可以通过为多个 NSTextView 创建一个 NSLayoutManager 和一个 NSTextStorag
[在底部更新了解决方案和工作代码] 在-viewDidLoad我分配,initWithFrame: 将 myTextView 添加到 subView 设置一些基本属性(对齐方式、背景颜色、文本颜色等)
我正在尝试使用 NSTextContainer 的 excludePaths 排除 UITextView 中的一个正方形,如下所示: NSTextStorage* textStorage = [[NS
我是一名优秀的程序员,十分优秀!