如何在 UITextView 中设置边距(padding)?

发布于 2024-09-09 00:33:56 字数 185 浏览 2 评论 0原文

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

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

有没有办法让一定宽度的 UITextView 显示具有较窄“换行宽度”的文本?

I have a UITextView for text editing. By default, it has a small margin around the text. I want to increase that margin by a few pixels.

The contentInset property gives me margins, but it does not change the text "wrap width". The text is wrapped at the same width, and the extra "margin" just causes the view to scroll horizontally.

Is there a way to make a UITextView of a certain width display the text with a narrower "wrap width"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

掐死时间 2024-09-16 00:33:56

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

Objective-C

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

Swift

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

Starting from iOS 7 you can use textContainerInset property:

Objective-C

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

Swift

textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
困倦 2024-09-16 00:33:56

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

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

对于左侧和右侧边距,不要添加普通的立即文本,但使用 NSAttributedString 代替,并使用 NSMutableParagraphStyle 正确设置左右缩进:

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 像素的填充,可以正确滚动。

After fiddling around with this for a while I found another solution if you're only developing for iOS 6. Set the top and bottom margins with contentInset:

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

For the left and right margins don't add your plain text right away but use an NSAttributedString instead with properly set left and right indents with an NSMutableParagraphStyle:

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

This gives you a UITextView with your text (in my case from the variable myText) with 20 pixels padding that properly scrolls.

椒妓 2024-09-16 00:33:56

试试这个

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

Try this

UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)];
txtView.textContainer.exclusionPaths  = @[aObjBezierPath];
巷子口的你 2024-09-16 00:33:56

您可以使用较小的 UITextView,并将 UIView 放置在背景中来模拟填充。

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

You could just use a smaller UITextView, and place a UIView in the background to simulate the padding.

+----------+
|          | <-- UIView (as background)
|   +--+   |
|   |  | <---- UITextView
|   |  |   |
|   +--+   |
|          |
+----------+
天涯离梦残月幽梦 2024-09-16 00:33:56

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

 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

if you want to set padding from one side, you can use below code:

 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
久伴你 2024-09-16 00:33:56

这对我有用。更改 textView contentInset 和框架插入/位置以获得正确的填充和自动换行。然后更改 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];
}

This is working for me. Changing the textView contentInset and frame inset/position to get the correct padding and word wrap. Then changing the textView bounds to prevent horizontal scrolling. You also need to reset the padding each time the text is selected and changed.

- (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];
}
心不设防 2024-09-16 00:33:56

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

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

Thanks for the suggestions.
I had this problem when developing a macOS / OSX app and ended up with this:

textView.textContainerInset = NSSize(width: 20, height: 20); //Sets margins
書生途 2024-09-16 00:33:56

尝试下面的代码

对于 iOS7 使用 textContainerInset

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

检查下面的链接,它可能对您有用

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

Try below code

For iOS7 use textContainerInset

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

Check below link it might be useful to you

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

扎心 2024-09-16 00:33:56

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

Swift + iOS 12: A different approach

Setting padding to the left + right side of our UITextView did let the text disappear when entered more that 9 lines of text. Using the solution of @rajesh helped to solve this issue:

Make sure to invoke the method whenever text did change + the device rotates.

    /// 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]
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文