問(wèn)題描述
我是 iOS 開(kāi)發(fā)的新手,但為了開(kāi)始使用垂直蒙古文腳本制作最基本的應(yīng)用程序,我需要一個(gè)垂直的 UITextView
.我還沒(méi)有找到任何其他蒙古應(yīng)用程序開(kāi)發(fā)人員共享他們的代碼,所以我正在嘗試自己制作.蒙古語(yǔ)從上到下書(shū)寫(xiě),從左到右換行,如下圖所示:
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:
滾動(dòng)應(yīng)該是水平的(從左到右).
Scrolling should be horizontal (left to right).
在 previous question 我提出了一個(gè)鏡像字體以及 UITextView
的旋轉(zhuǎn)和鏡像的組合(因?yàn)檫@是我在 Android 中所做的).但是,在閱讀 this answer 并進(jìn)一步研究 iOS 的做事方式(如使用 TextKit
),我想它可能最好從頭開(kāi)始創(chuàng)建像 UIVerticalTextView
這樣的東西,而不是弄亂普通的 UITextView
.因?yàn)槲铱吹?UITextView
繼承自 UIScrollView,我假設(shè)我應(yīng)該繼承 UIScrollView
來(lái)制作 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
的子類(lèi),但我在獲取時(shí)遇到了很多麻煩滾動(dòng)上班.這是我想繼承 UIScrollView
的另一個(gè)原因.此外,當(dāng)方向發(fā)生變化時(shí),該解決方案不會(huì)重新排列每行中的單詞.
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 - 應(yīng)該有這沒(méi)什么不同.它只會(huì)存儲(chǔ)蒙古語(yǔ) Unicode 文本和任何樣式信息.
- NSTextContainer - 高度并且需要切換寬度以給出垂直文本容器的大小.(無(wú)論如何,我認(rèn)為是的.除非這可以在其他地方完成.)
- NSLayoutManager - 什么都做特別需要在這里完成還是由
UIVerticalTextView
處理? UIVerticalTextView - 這將是
UIScrollView
的子類(lèi),但需要添加和/或覆蓋哪些方法?
- 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
}
這些問(wèn)題試圖將問(wèn)題分解成更小的部分:
These questions are attempts to break the problem down into smaller pieces:
- 如何從頭開(kāi)始制作 UITextView?
- 如何在 Swift 中使用字符串初始化 NSTextStorage一個(gè)>
并嘗試不同的方法:
- 旋轉(zhuǎn)視圖是否與自動(dòng)布局兼容?
- 在 layoutSubviews 中旋轉(zhuǎn)視圖這實(shí)際上現(xiàn)在有一個(gè)可行的解決方案.但是,它基本上涉及將三個(gè)視圖堆疊在一起.我仍然希望創(chuàng)建一個(gè)使用 TextKit 的自定義 UIScrollView 子類(lèi),就像我在當(dāng)前問(wèn)題中提出的那樣.
- 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.
推薦答案
如果你有一個(gè)顯示文本的字體,最簡(jiǎn)單的方法是使用普通的 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
現(xiàn)在將向右滾動(dòng)而不是向下滾動(dòng),我認(rèn)為這應(yīng)該發(fā)生在蒙古語(yǔ)中?
The textView
will now scroll to the right not down, I assume that is supposed to happen for mongolian?
這篇關(guān)于如何通過(guò)從 Swift 中的 UIScrollView 子類(lèi)化來(lái)制作垂直 UITextView?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!