UITextView contentInset无法正常工作

时间:2014-04-08 11:14:33

标签: ios objective-c ios6 uitextview uiedgeinsets

我在UITextView中显示文字。我需要对文本应用一些填充。当我使用价值作为顶部位置时,它正在工作。如果我将值用于其他位置则不起作用。

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

4 个答案:

答案 0 :(得分:3)

对于iOS7,请使用textContainerInset

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

对于Bellow iOS7,请使用contentInset并将UIEdgeInsetsMake设置为以下语法。

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

根据您的代码,您只设置顶部插入但如果您想设置所有侧面,您必须设置内容插入如下: -

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

看起来像这样: -

enter image description here

答案 1 :(得分:2)

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

答案 2 :(得分:1)

使用此:

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

此外,定位元素的正确方法是layoutSubviews方法。

答案 3 :(得分:0)

另一种可能的解决方案如下:

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

我收到了一个不受欢迎的行为,其中textContentInset正确地放置了文本,但随后内容将滚出屏幕。