如何通过从 Swift 中的 UIScrollView 子类化来制作垂

时间:2023-02-15
本文介绍了如何通过从 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).

在 previous question 我提出了一个镜像字体以及 UITextView 的旋转和镜像的组合(因为这是我在 Android 中所做的).但是,在阅读 this answer 并进一步研究 iOS 的做事方式(如使用 TextKit),我想它可能最好从头开始创建像 UIVerticalTextView 这样的东西,而不是弄乱普通的 UITextView.因为我看到 UITextView 继承自 UIScrollView,我假设我应该继承 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:

  • 如何从头开始制作 UITextView?
  • 如何在 Swift 中使用字符串初始化 NSTextStorage

并尝试不同的方法:

  • 旋转视图是否与自动布局兼容?
  • 在 layoutSubviews 中旋转视图这实际上现在有一个可行的解决方案.但是,它基本上涉及将三个视图堆叠在一起.我仍然希望创建一个使用 TextKit 的自定义 UIScrollView 子类,就像我在当前问题中提出的那样.
  • 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:是否可以将 UIScrollView 指示器从右侧移动到左侧 下一篇:scrollViewDidScroll 从未调用过

相关文章

最新文章