带有多个NSTextContainers的NSLayoutManager导致UITextViews不可选择/不可编辑 [英] NSLayoutManager with multiple NSTextContainers causes UITextViews to not be selectable/editable

查看:128
本文介绍了带有多个NSTextContainers的NSLayoutManager导致UITextViews不可选择/不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to achieve a multi page text editing layout, as in Pages, MS Word, ... . On OS X I can achieve this by creating one NSLayoutManager with one NSTextStorage for multiple NSTextViews. Each NSTextView has its own NSTextContainer. See below code for OS X. A simple example with text spreading between two NSTextViews:

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进行相同操作时,UITextViews变得不可选择或不可编辑.查看我的iOS代码:

However, when I try to do the same on iOS with UITextView, the UITextViews become not selectable or editable. See my code for 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流到另一个,但不可编辑.我将非常感谢您的任何建议.我搜索了这个问题,发现其他人也遇到了同样的问题,但是没有找到解决方法.

The text flows from one UITextView to the other, but it is not editable. I would be extremely thankful for any advice. I googled the issue and found other people experiencing the same problem, but found no solutions.

推荐答案

我也一直在寻找答案,似乎当UITextView与其他文本视图共享布局管理器时,该文本视图不幸地变成静态的.

I've been searching for an answer too, and it seems that when a UITextView shares a layout manager with other text views, the text view unfortunately becomes static.

iOS 7编程突破极限:第375页,开发适用于Apple的高级应用程序中概述了此问题:

因为您可以轻松地将其他文本容器添加到布局中 管理器,并且因为UITextView使用布局管理器,所以您可能会认为 创建一个多列,可编辑的UITextView很容易. 不幸的是,这种情况并非如此.如果分配了UITextView 多个文本容器[原文如此],它变为静态并且无法响应 用户交互(例如编辑或选择).这是已知的 问题,目前是按设计要求".

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".

这篇关于带有多个NSTextContainers的NSLayoutManager导致UITextViews不可选择/不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆