You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UITextView禁用isScrollEnabled后右侧contentInset消失问题求助

解决UITextView禁用滚动后右侧contentInset失效的问题

当UITextView的isScrollEnabled设为false时,系统布局逻辑会调整:文本内容自动适配textView宽度,无需滚动,导致contentInset的右侧设置被忽略。如果不想使用textContainerInset,可通过以下两种方案解决:

方案一:用容器视图模拟内边距

放弃直接给textView设置contentInset,将textView嵌套在容器视图中,通过容器内的约束实现边距效果,同时把边框设置在容器上:

class ViewController: UIViewController {
    
    var textViewContainer: UIView!
    var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray
        setupTextViewContainer()
        setupTextView()
        setupContraints()
    }
    
    private func setupTextViewContainer() {
        textViewContainer = UIView()
        textViewContainer.layer.borderColor = UIColor.red.cgColor
        textViewContainer.layer.borderWidth = 2
        view.addSubview(textViewContainer)
    }
    
    private func setupTextView() {
        textView = UITextView()
        textView.text = "Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS."
        textView.font = .boldSystemFont(ofSize: 24)
        textView.isScrollEnabled = false
        textViewContainer.addSubview(textView)
    }
    
    private func setupContraints() {
        textViewContainer.translatesAutoresizingMaskIntoConstraints = false
        textViewContainer.heightAnchor.constraint(equalToConstant: 250).isActive = true
        textViewContainer.widthAnchor.constraint(equalToConstant: 350).isActive = true
        textViewContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        textViewContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
        textView.translatesAutoresizingMaskIntoConstraints = false
        // 用约束模拟contentInset的四边间距
        textView.topAnchor.constraint(equalTo: textViewContainer.topAnchor, constant: 25).isActive = true
        textView.leftAnchor.constraint(equalTo: textViewContainer.leftAnchor, constant: 25).isActive = true
        textView.bottomAnchor.constraint(equalTo: textViewContainer.bottomAnchor, constant: -25).isActive = true
        textView.rightAnchor.constraint(equalTo: textViewContainer.rightAnchor, constant: -25).isActive = true
    }
}

方案二:重写UITextView的layoutSubviews方法

自定义UITextView子类,手动调整contentSize宽度,确保右侧contentInset生效:

class CustomTextView: UITextView {
    override func layoutSubviews() {
        super.layoutSubviews()
        guard !isScrollEnabled else { return }
        // 强制contentSize宽度等于textView宽度减去左右contentInset
        let targetWidth = bounds.width - contentInset.left - contentInset.right
        if contentSize.width != targetWidth {
            contentSize = CGSize(width: targetWidth, height: contentSize.height)
        }
    }
}

// 使用自定义TextView
class ViewController: UIViewController {
    
    var textView: CustomTextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray
        setupTextView()
        setupContraints()
    }
    
    private func setupTextView() {
        textView = CustomTextView()
        textView.text = "Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS."
        textView.font = .boldSystemFont(ofSize: 24)
        textView.contentInset = .init(top: 25, left: 25, bottom: 25, right: 25)
        textView.isScrollEnabled = false
        textView.layer.borderColor = UIColor.red.cgColor
        textView.layer.borderWidth = 2
        view.addSubview(textView)
    }
    
    private func setupContraints() {
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.heightAnchor.constraint(equalToConstant: 250).isActive = true
        textView.widthAnchor.constraint(equalToConstant: 350).isActive = true
        textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
}

两种方案均无需依赖textContainerInset,其中方案一兼容性更好,不会干扰UITextView原有布局逻辑。

内容的提问来源于stack exchange,提问作者FE_Tech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 16:50:27