How to make a vertical UITextView by subclassing i

2019-02-05 23:06发布

Background

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:

enter image description here

Scrolling should be horizontal (left to right).

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.

Where I am at now

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 - 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
    }
    

Updates

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

And trying with a different approach:

1条回答
你好瞎i
2楼-- · 2019-02-05 23:20

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]]];

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

查看更多
登录 后发表回答