4

我在 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

4 回答 4

6

对于 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);
    }

看起来像这样:-

在此处输入图像描述

于 2014-04-08T11:19:45.173 回答
2

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

于 2021-01-20T11:48:41.033 回答
1

用这个:

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

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

于 2014-04-08T11:19:33.160 回答
0

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

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

我在开始时正确放置文本时遇到了不受欢迎的行为textContentInset,但随后内容会滚动到屏幕外。

于 2016-04-06T04:16:38.227 回答