問題描述
我是 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模板網!