UITextView contentInset not working

I output text inside a UITextView. I need to apply some additions to the text. When I use the value for the top position, it works. If I use the value for another position, it does not work.

txtt_bgImage = [UIImage imageNamed:@"txtt_bg_ipad.png"]; txtt_bgImageView = [[UIImageView alloc] initWithImage:txtt_bgImage]; txtt=[[UITextView alloc]initWithFrame:CGRectMake(170, 300, 400, 250)]; txtt.text=productDescription; [txtt setFont: [UIFont fontWithName:@"verdana" size:15]]; txtt.textColor=[UIColor whiteColor]; [txtt setBackgroundColor:[UIColor clearColor]]; txtt.contentInset = UIEdgeInsetsMake(15,0,0,0); // [txtt setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)]; txtt_bgImageView.frame=CGRectMake(170, 300, 400, 250); [self.view addSubview:txtt_bgImageView]; txtt.editable=NO; NSLog(@"text is %@",txtt.text); object.object_screentext = txtt; [self.view addSubview:txtt]; 
+1
ios objective-c ios6 uitextview uiedgeinsets
Apr 08 '14 at 11:14
source share
3 answers

For iOS7 use textContainerInset

 @property(nonatomic, assign) UIEdgeInsets textContainerInset NS_AVAILABLE_IOS(7_0); 

For Bellow iOS7, use contentInset and set UIEdgeInsetsMake as the syntax below.

 UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) { UIEdgeInsets insets = {top, left, bottom, right}; return insets; } 

According to your code, you only install the top insert. But if you want to set all sides, you need to set the content insert, for example, below: -

 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { _TxtViewSummary.contentInset = UIEdgeInsetsMake(10, 0, 10, 0); } else { _TxtViewSummary.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10); } 

It looks like this: -

enter image description here

+2
Apr 08 '14 at
source share

Use this:

 [txtt setTextContainerInset:UIEdgeInsetsMake(7, 7, 0, 0)]; 

In addition, the correct way to position an element is in the layoutSubviews method.

+1
Apr 08 '14 at
source share

Another possible solution is the following:

 self.textView.contentInset = UIEdgeInsets(top: 740.0, left: 0, bottom: 20.0, right: 0) self.textView.contentOffset = CGPoint(x: 0, y: -740) 

I was getting unwanted behavior when textContentInset correctly positioned the text, but then the contents scroll from the screen.

0
Apr 6 '16 at 4:16
source share