How to set a margin in UITextView?

10,848

Solution 1

You can use the property textContainerInset

Following the example of George Green:

myTextView.textContainerInset = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);

Solution 2

UITextView is a subclass of UIScrollView. I haven't tried it but you could try something like:

myTextView.contentInset = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);

UIEdgeInsetsMake() is as follows:

UIEdgeInsets UIEdgeInsetsMake (
   CGFloat top,
   CGFloat left,
   CGFloat bottom,
   CGFloat right
);

So you should be able to inset your textView content.

Hope this helps, let me know if it works!! :)

Solution 3

I wanted to do the same thing, but textview.contentInset didn't work.

I put UITextView with a narrow width on UIView, then move the scrollview indicator of textview to the right side, so I got what I wanted really well.

textView.clipsToBounds = NO; textView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, -20.0f);

This answer might help. Is there a way to put UITextView's scroll indicator to outside UITextView?

Solution 4

You can set the scroller right inset value of the UITextView to negative value and disable the clip subview option to achieve your require. No other scrollview is needed. Select textview on storyboard first

In code it would be

textView.clipsToBounds = NO;
textView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, -50.0f);
Share:
10,848

Related videos on Youtube

pnmn
Author by

pnmn

Updated on March 16, 2020

Comments

  • pnmn
    pnmn about 4 years

    I'm currently developing a simple text-editing app for iPad. I want to set left/right margins like the attached picture. Just adding UITextView into another UIView with larger width won't work because a scroll indicator won't be properly located.

    Instead of UIView, I added UITextView into UIScrollView, and it works almost fine. But they sometimes show strange behaviors, and UITextViewDelegate doesn't work with my UIViewController.

    Is there any way to set left/right margins only using UITextView? Thank you.

    alt text

  • Victor Engel
    Victor Engel over 11 years
    This doesn't quite work for me. Sure, the margins are added, but they are added outside the bounds of the UITextView. The result is a text display that starts out flush with the edge, but you can scroll it out to the margin. What am I missing?