Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding left margin to UITextView

I'm trying to add a left margin to a UITextView.

I've tried setting the property contentInset, see below:

UITextView *textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
textView.editable = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
textView.backgroundColor = [UIColor clearColor];
textView.opaque = NO;
textView.contentInset = UIEdgeInsetsMake(0, 15.0f, 0, 0);

This seems to work though it causes horizontal scrolling on the TextView which I don't want.

I just want the text inset on the left without making the textview any wider.

like image 926
Camsoft Avatar asked Aug 10 '11 11:08

Camsoft


2 Answers

Remember that for iOS 7 there a special property called

textContainerInset The inset of the text container's layout area within the text view's content area.

@property(nonatomic, assign) UIEdgeInsets textContainerInset

This property provides text margins for text laid out in the text view.

Availability Available in iOS 7.0 and later. Declared In UITextView.h

in iOS 6 the contentInset is doing the job. I personally encountered this problem. Everything ended up with:

if (isIOS7()) {
    textView.textContainerInset = UIEdgeInsetsMake(0, 10, 0, 10);
} else {
    textView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
}
like image 197
Dumoko Avatar answered Oct 02 '22 21:10

Dumoko


If you only want to move the text, try

[textView setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];

Where a positive number moves the "text frame" towards the middle, a negative moves it out from the middle.

For example, [textView setContentInset:UIEdgeInsetsMake(0, 20, 0,-20)], will move the text 20 pixels to the right!

like image 44
Karl Eriksson Avatar answered Oct 02 '22 23:10

Karl Eriksson