UITextView contentInset is not working
Asked Answered
C

4

4

I'm displaying text inside UITextView. I need to apply some padding to text. When i use value for top position it's working. If i use value for other position it's not working.

 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];
Crittenden answered 8/4, 2014 at 11:14 Comment(2)
possible duplicate of UITextView contentInset not working in UITextView on iOS 7?Fornication
I'm not using iOS7. I referred that question already it's not workingCrittenden
I
6

For iOS7 use textContainerInset

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

For Bellow iOS7 use contentInset and setting UIEdgeInsetsMake as bellow syntax.

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 are setting only top side inset But if you wish to set all side you have to set content inset like bellow :-

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

That look like this:-

enter image description here

Irtysh answered 8/4, 2014 at 11:19 Comment(7)
i already mention that used for ios7 with Xcode5 if you are still working with old one use _TxtViewSummary.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);Irtysh
I'm using iOS6.1 then why contentInset is not workingCrittenden
at my end that working code check that IBOutlate or textviewl connect correctly or not.Irtysh
I'm not using IBOutlet. I'm using code what i asking in questionCrittenden
txtt.contentInset = UIEdgeInsetsMake(15,0,0,0); that put only top side space not left or right sir replce with (10, 0, 10, 0)Irtysh
(10, 10, 10, 10) put this first 10 for top second for left third for bottom and forth for right.Irtysh
If i use negative value like -20 it's working with reverse. why it's not working in positive value?Crittenden
P
3

textView.textContainerInset = UIEdgeInsets(top: 14, left: 16, bottom: 14, right: 16)

Plashy answered 20/1, 2021 at 11:48 Comment(0)
L
1

Use this:

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

Also, the right way to position element is in layoutSubviews method.

Lubow answered 8/4, 2014 at 11:19 Comment(2)
No visible @Interface 'UITextView' declares the selector setTextContainerInsetCrittenden
This is for iOS7.prior to iOS 7 versions you need to use contentInset.Lubow
S
0

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 an unwanted behaviour where textContentInset was placing the text properly to begin with, but then the content would scroll off screen.

Sterner answered 6/4, 2016 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.