Add “padding” to a UITextView

前端 未结 8 520
渐次进展
渐次进展 2021-01-31 14:19

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 c

相关标签:
8条回答
  • 2021-01-31 14:42

    This is working in Swift 5:

    textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    
    0 讨论(0)
  • 2021-01-31 14:50

    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); 
    
    0 讨论(0)
  • 2021-01-31 14:50

    For Swift 4.2:

    textview.contentInset = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)
    
    0 讨论(0)
  • 2021-01-31 14:50

    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() 
    
    0 讨论(0)
  • 2021-01-31 14:52

    This worked for me using swift 2.0:

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

    0 讨论(0)
  • 2021-01-31 14:54

    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)

    0 讨论(0)
提交回复
热议问题