【问题标题】:How to set margins (padding) in UITextView?如何在 UITextView 中设置边距(填充)?
【发布时间】:2011-03-14 23:57:57
【问题描述】:

我有一个用于文本编辑的 UITextView。默认情况下,它在文本周围有一个小边距。我想将该边距增加几个像素。

contentInset 属性为我提供了边距,但它不会更改文本“换行宽度”。文本以相同的宽度换行,额外的“边距”只会导致视图水平滚动。

有没有办法让某个宽度的 UITextView 显示“换行宽度”更窄的文本?

【问题讨论】:

标签: iphone objective-c cocoa-touch ipad uitextview


【解决方案1】:

您可以只使用较小的 UITextView,并在后台放置一个 UIView 来模拟填充。

+----------+
|          | <-- UIView (as background)
|   +--+   |
|   |  | <---- UITextView
|   |  |   |
|   +--+   |
|          |
+----------+

【讨论】:

  • 是的,这就是我现在正在做的事情。侧边距工作正常,但上边距有问题。从用户的角度来看,文本在滚动时在顶部被“截断”,因为文本只在 UITextView 中呈现。
  • 这样你不能正确显示垂直滚动条。
  • 设置 textView.clipsToBounds = NO 将“修复”顶部的切断,有点。当然,这无助于正确显示垂直滚动条。
【解决方案2】:

这对我有用。更改 textView contentInset 和 frame inset/position 以获得正确的填充和自动换行。然后更改 textView 边界以防止水平滚动。每次选择和更改文本时,您还需要重置填充。

- (void)setPadding
{
    UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50);

    textView.contentInset = padding;

    CGRect frame = textView.frame;

    // must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets
    CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right));

    // offset frame back to original x
    CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - ( padding.left + padding.right ) / 2);
    insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0));

    textView.frame = insetFrame;

    textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right));

    [textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}

-(void)textViewDidChangeSelection:(UITextView *)textView
{
    [self setPadding];
}

-(void)textViewDidChange:(UITextView *)textView
{
    [self setPadding];
}

【讨论】:

  • @Nate 这对我不起作用。 textview 仍然水平滚动,并且内容插入在文本视图的框架之外刷新
【解决方案3】:

如果你只是为 iOS 6 开发,在摆弄了一段时间后,我找到了另一个解决方案。使用 contentInset 设置顶部和底部边距:

textView = [[UITextView alloc] init];
textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);

对于左右边距,不要立即添加纯文本,而是使用 NSAttributedString 代替正确设置左右缩进和 NSMutableParagraphStyle:: p>

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 20.0;
paragraphStyle.firstLineHeadIndent = 20.0;
paragraphStyle.tailIndent = -20.0;

NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary];

这为您提供了一个 UITextView,其中包含您的文本(在我的情况下来自变量 myText),具有 20 像素的填充,可以正确滚动。

【讨论】:

    【解决方案4】:

    从 iOS 7 开始你可以使用textContainerInset 属性:

    Objective-C

    textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20);
    

    斯威夫特

    textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    

    【讨论】:

    • 也可以只添加左右 textContainerInsetstextView.textContainerInset.left = 20 textView.textContainerInset.right = 20
    【解决方案5】:

    试试这个

    UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)];
    txtView.textContainer.exclusionPaths  = @[aObjBezierPath];
    

    【讨论】:

      【解决方案6】:

      感谢您的建议。 我在开发 macOS / OSX 应用程序时遇到了这个问题,结果是这样的:

      textView.textContainerInset = NSSize(width: 20, height: 20); //Sets margins
      

      【讨论】:

        【解决方案7】:

        试试下面的代码

        对于 iOS7 使用 textContainerInset

        textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20);

        查看下面的链接,它可能对你有用

        https://stackoverflow.com/a/22935404/5184217

        【讨论】:

          【解决方案8】:

          Swift + iOS 12不同的方法

          UITextView 的左侧+右侧设置填充确实让文本在输入超过 9 行文本时消失。使用@rajesh 的解决方案帮助解决了这个问题:

          确保在文本发生变化 + 设备旋转时调用该方法。

              /// Updates the left + right padding of the current text view.
              /// -> leftRightPadding value = 11.0
              func updateLeftRightPadding() {
                  let leftPadding = UIBezierPath(rect: .init(x: 0.0, y: 0.0,
                                                 width: leftRightPadding, height: contentSize.height))
                  let rightPadding = UIBezierPath(rect: .init(x: frame.width - leftRightPadding, y: 0.0,
                                                  width: 11, height: contentSize.height))
                  textContainer.exclusionPaths = [leftPadding, rightPadding]
              }
          

          【讨论】:

            【解决方案9】:

            如果你想从一侧设置填充,你可以使用下面的代码:

             textView.contentInset.left = 5 //For left padding
             textView.contentInset.right = 5 //For right padding
             textView.contentInset.top = 5 //For top padding
             textView.contentInset.bottom = 5 //For bottom padding
            

            【讨论】:

              猜你喜欢
              • 2010-10-19
              • 2011-05-19
              • 2018-08-03
              • 1970-01-01
              • 2011-03-31
              • 1970-01-01
              • 1970-01-01
              • 2015-10-16
              • 1970-01-01
              相关资源
              最近更新 更多