添加“填充”到 UITextView

发布于 2024-12-12 04:18:16 字数 802 浏览 0 评论 0原文

正如标题所说,我正在尝试向 UITextView 添加类似填充的行为。当视图被推入我的导航控制器并发生以下代码时,会生成文本视图:

 self.textView.layer.cornerRadius = 7;
 //pretty stuff is pretty

 NSString *description = appDelegate.productInDisplay.description;
 [self.textView setText:description];
 //pretty stuff has content now


 CGRect frame = textView.frame;
 frame.size.height = textView.contentSize.height;
 textView.frame = frame;
 //set the UITextView to the size of it's containing text.

 self.textView.editable = NO;

  self.theScrollView.contentSize = CGSizeMake(320, 250 + textView.frame.size.height);
  //set the parent scrollView's height to fit some other elements with fixed size (250)
  //and the pre-mentioned UITextView

所以,一切正常,但我想在 UITextView 的所有 4 边添加一些填充,但我无法做到经过 3 个小时的谷歌搜索,找到了一些看起来相当简单的东西。有什么建议吗?

as the title says i am trying to add padding-like behavior to a UITextView. The textview is generated when the view is pushed inside my navigation controller and the following code occurs:

 self.textView.layer.cornerRadius = 7;
 //pretty stuff is pretty

 NSString *description = appDelegate.productInDisplay.description;
 [self.textView setText:description];
 //pretty stuff has content now


 CGRect frame = textView.frame;
 frame.size.height = textView.contentSize.height;
 textView.frame = frame;
 //set the UITextView to the size of it's containing text.

 self.textView.editable = NO;

  self.theScrollView.contentSize = CGSizeMake(320, 250 + textView.frame.size.height);
  //set the parent scrollView's height to fit some other elements with fixed size (250)
  //and the pre-mentioned UITextView

so, it all works and it's ok, but i want to add some padding on all 4 sides of the UITextView and i've been unable to do this with 3 hours of googling for something that seems rather easy. Any suggestions?

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

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

发布评论

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

评论(9

岁月打碎记忆 2024-12-19 04:18:16

使用 Objective-C 我已经用

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];

For Swift 完成了它,您可以使用:

textview.contentInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)

您还可以创建一个 Swift 扩展 (由 chowdhury-md-rajib-sarwar) 此处:

extension UITextView {
func leftSpace() {
    self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
}

}

然后使用

let textView = UITextView()
textView.leftSpace() 

Using Objective-C I have done it just with

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];

For Swift you can use:

textview.contentInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)

You can also create a Swift Extension (Suggested by chowdhury-md-rajib-sarwar) here:

extension UITextView {
func leftSpace() {
    self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
}

}

And then use

let textView = UITextView()
textView.leftSpace() 
吃不饱 2024-12-19 04:18:16

这个答案是完全错误的。正确的答案很简单:(

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right

这些值通常与同一屏幕上的 UITextField 的外观相匹配。)


使用这个:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 

This answer is totally wrong. Correct answer is simply:

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right

(Those values generally match how a UITextField on the same screen looks.)


Use this one:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 
一袭白衣梦中忆 2024-12-19 04:18:16

这在 Swift 5 中有效:

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

This is working in Swift 5:

textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
戴着白色围巾的女孩 2024-12-19 04:18:16

编辑 2015 年 1 月 16 日:这是 2012 年写的,不再准确。如上所述使用 -textContainerInset。

原始帖子:

使用 contentInset 实际上不起作用。您有两个合理的选择:子类 UITextField 并重写 textRectForBounds: 和 EditingRectForBounds: 方法,或者在 UIView 上透明地创建文本字段并设置 UIView 的样式。

子类化 UITextField 的示例:

- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

将其包装在 UIView 中的示例:

UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
[wrapView addSubview:textView];
wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
wrapView.layer.borderWidth = 2.0;
wrapView.layer.cornerRadius = 5.0;

EDIT 1/16/2015: This was written in 2012 and is no longer accurate. Use -textContainerInset as mentioned above.

Original post:

Using contentInset won't actually work. You have two reasonable choices: subclass UITextField and override the textRectForBounds: and editingRectForBounds: methods or create your text field transparently over a UIView and style the UIView.

Example of subclassing the UITextField:

- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

Example of wrapping it in a UIView:

UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
[wrapView addSubview:textView];
wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
wrapView.layer.borderWidth = 2.0;
wrapView.layer.cornerRadius = 5.0;
像你 2024-12-19 04:18:16

这对我使用 swift 2.0 有用:

textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)

This worked for me using swift 2.0:

textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)

地狱即天堂 2024-12-19 04:18:16

对于斯威夫特 4.2:

textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

For Swift 4.2:

textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
一桥轻雨一伞开 2024-12-19 04:18:16

对于 Swift 3 - 答案似乎相似,但略有不同:(

textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

实际上,我将上面的内容用于 UIButtonView,但由于它与为 UITextView 发布的答案非常接近,我想[希望]它适用也为此)

For Swift 3 - the answer appears to be similar, but slightly different:

textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

(Actually, I used the above for a UIButtonView, but as it is so closed to the answers that were posted for UITextView I'm thinking [hoping] that it applies for that too)

烂柯人 2024-12-19 04:18:16

只需使用 Container Edge Inset 创建 UITextView扩展,如下所示:

extension UITextView {
    func leftSpace() {
        self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
    }
}

并使用它:

let textView = UITextView()
textView. leftSpace() 

Simply create an extension of UITextView using Container Edge Inset as following:

extension UITextView {
    func leftSpace() {
        self.textContainerInset = UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 4)
    }
}

and use it like:

let textView = UITextView()
textView. leftSpace() 
仅一夜美梦 2024-12-19 04:18:16

textContainerInset - 应该设置,并且 lineFragmentPadding 应设置为 0 以删除默认填充

 override func viewDidLoad() {
        super.viewDidLoad()
        
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "This is a sample text for the UITextView."
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.backgroundColor = UIColor.lightGray
        
        // Remove top and bottom padding
        textView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        textView.textContainer.lineFragmentPadding = 0
        
        view.addSubview(textView)
        
        // Set up constraints
        NSLayoutConstraint.activate([
            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            textView.heightAnchor.constraint(equalToConstant: 200)
        ])
    }

textContainerInset - should be set and lineFragmentPadding should be set to 0 for removing default padding

 override func viewDidLoad() {
        super.viewDidLoad()
        
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "This is a sample text for the UITextView."
        textView.font = UIFont.systemFont(ofSize: 16)
        textView.backgroundColor = UIColor.lightGray
        
        // Remove top and bottom padding
        textView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        textView.textContainer.lineFragmentPadding = 0
        
        view.addSubview(textView)
        
        // Set up constraints
        NSLayoutConstraint.activate([
            textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            textView.heightAnchor.constraint(equalToConstant: 200)
        ])
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文