如何通过从Swift中的UIScrollView继承它来创建一个垂直的UITextView? [英] How to make a vertical UITextView by subclassing it from UIScrollView in Swift?

查看:172
本文介绍了如何通过从Swift中的UIScrollView继承它来创建一个垂直的UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,但为了开始使用垂直蒙古语脚本制作最基本的应用程序,我需要有一个垂直的UITextView 。我没有找到任何其他蒙古应用程序开发人员共享他们的代码所以我试图自己做。蒙古语是从上到下书写的,线条从左到右换行,如下图所示:

I'm new to iOS development, but in order start making even the most rudimentary apps using the vertical Mongolian script, I need to have a vertical UITextView. I haven't found any other Mongolian app developers sharing their code so I am trying to make it myself. Mongolian is written from top to bottom and lines wrap from left to right as is illustrated in the following graphic:

滚动应该是水平的(从左到右)。

Scrolling should be horizontal (left to right).

上一个问题我提出了镜像字体和 UITextView 的旋转和镜像的组合(因为这是我在Android中所做的) )。但是,在阅读这个答案并进一步研究iOS的做事方式后(比如使用 TextKit ),我认为从头开始创建类似 UIVerticalTextView 的东西可能会更好,而不是搞乱普通的 UITextView 。因为我看到 UITextView 来自UIScrollView的inheirits ,我假设我应该继承 UIScrollView 来制作 UIVerticalTextView

In a previous question I proposed a combination of a mirrored font and a rotation and mirroring of the UITextView (because that is what I had done in Android). However, after reading this answer and doing further research into the iOS way of doing things (like using TextKit), I'm thinking it may be better to just create something like UIVerticalTextView from scratch rather than messing with the normal UITextView. And since I see that UITextView inheirits from UIScrollView, I assume I should subclass UIScrollView to make the UIVerticalTextView.

我上面提到的答案子类 UIView 但是我在滚动工作时遇到了很多麻烦。这是我想要继承 UIScrollView 的另一个原因。当方向发生变化时,该解决方案也没有重新排除每行中的单词。

The answer I mentioned above subclassed UIView but I was having a lot of trouble getting scrolling to work. This is another reason I want to subclass UIScrollView. Also that solution did not relayout the words in each line when there was an orientation change.

我收集了以下内容,但我不知道如何放置它们在一起:

I've gathered the following pieces but I'm not sure how to put them together:


  • NSTextStorage - 不应该有任何不同之处。它只会存储蒙古语Unicode文本和任何样式信息。

  • NSTextContainer - 需要切换高度和宽度以提供垂直文本容器大小。 (我想是的,无论如何。除非这可以在其他地方完成。)

  • NSLayoutManager - 这里需要做什么特别的事情,或者由 UIVerticalTextView

  • UIVerticalTextView - 这将是 UIScrollView 的子类但是什么是需要添加和/或覆盖的方法?

  • NSTextStorage - There shouldn't be anything different with this. It will just store the Mongolian Unicode text and any styling information.
  • NSTextContainer - The height and the width need to be switched to give the vertical text container size. (I think so, anyway. Unless this can be done somewhere else.)
  • NSLayoutManager - Does anything special need to be done here or is that taken care of by the UIVerticalTextView?
  • UIVerticalTextView - This will be a subclass of UIScrollView but what are the methods that need to be added and/or overridden?

class UIVerticalTextView: UIScrollView {

    // Which properties to include?
    //     - TextStorage
    //     - TextContainer
    //     - LayoutManager

    // which methods to override?

    // which methods to add to give minimal functionality like UITextView?
    //     - init
    //     = text
}


这些问题试图将问题分解成更小的部分:

These questions are attempts to break the problem down into smaller pieces:

  • How to make UITextView from scratch?
  • How to Initialize NSTextStorage with a String in Swift

尝试使用其他方法:

  • Are rotated Views compatible with Auto Layout?
  • Rotating a view in layoutSubviews This actually has a working solution now. However, it basically involves stacking three views on top of each other. I would still prefer to make a custom UIScrollView subclass that uses TextKit as I have presented in my current question.

推荐答案

如果您有一个显示文本的字体,最简单的方法是使用普通的UITextView:

If you have a font that displays your text the easiest way would be to use the normal UITextView:

UITextView *textView = [[UITextView] alloc] init];

textView.transform = CGAffineTransformMakeRotation(-M_PI/2);

textView.text = @"YOUR TEXT HERE";

[textView setBaseWritingDirection:UITextWritingDirectionRightToLeft forRange:[textView textRangeFromPosition:[textView beginningOfDocument] toPosition:[textView endOfDocument]]];

textView 现在滚动到右边没下来,我认为这应该发生在蒙古人身上?

The textView will now scroll to the right not down, I assume that is supposed to happen for mongolian?

这篇关于如何通过从Swift中的UIScrollView继承它来创建一个垂直的UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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