ios - Label 和 TextView 水平溢出 Scroll View
问题描述
我有一个滚动视图,其中有一个堆栈视图。在堆栈视图中,我安排了 UITextView 或 UILabel 元素的子视图。一切都是以编程方式完成的,没有故事板。
滚动视图出现,我可以很好地滚动它。但不幸的是,它不仅垂直滚动(从上到下)而且水平滚动(向右,屏幕外),这是我不想滚动的(这就是我在 UILabel 上设置 numberOfLines 的原因,试图设置相等的宽度到滚动和堆栈视图,因为堆栈视图的左/右属性连接到视图)。
如果它很重要,则在 viewDidLoad 或稍后触摸按钮时调用此函数。
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
let leftConstraintScroll = NSLayoutConstraint(item: scrollView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0)
let rightConstraintScroll = NSLayoutConstraint(item: scrollView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)
let topConstraintScroll = NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: selectedTabIndicator, attribute: .bottom, multiplier: 1, constant: 10)
let bottomConstraintScroll = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: editButton, attribute: .top, multiplier: 1, constant: 0)
view.addConstraints([leftConstraintScroll, rightConstraintScroll, topConstraintScroll, bottomConstraintScroll])
stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 10
stackView.isLayoutMarginsRelativeArrangement = true
stackView.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10)
// Several elements are added like this (UITextView):
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.delegate = self
textView.isScrollEnabled = false
textView.font = UIFont.systemFont(ofSize: 15)
textView.backgroundColor = Constants.COLOR_P
textView.textColor = .black
textView.text = "XXX"
stackView.addArrangedSubview(textView)
// Or UILabel:
var label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.textAlignment = .justified
label.textColor = .black
label.font = UIFont.systemFont(ofSize: 15)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.hyphenationFactor = 1.0
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.headIndent = 15
let hyphenAttribute = [NSAttributedString.Key.paragraphStyle: paragraphStyle]
let attributedString = NSMutableAttributedString(string: "XXXXX", attributes: hyphenAttribute)
label.attributedText = attributedString
stackView.addArrangedSubview(label)
scrollView.addSubview(stackView)
let leftConstraint = NSLayoutConstraint(item: stackView, attribute: .left, relatedBy: .equal, toItem: scrollView, attribute: .left, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: stackView, attribute: .right, relatedBy: .equal, toItem: scrollView, attribute: .right, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: 0)
scrollView.addConstraints([leftConstraint, rightConstraint, topConstraint, bottomConstraint, bottomConstraint])
注意: selectedTabIndicator 和 editButton 分别位于滚动视图的上方和下方。
解决方案
我可以解决它,也许不是最好的解决方案,所以我把这个问题留了一段时间,以获得更好的解决方案。
基本上,我在创建它们时为每个 UILabel 和 UITextView 提供了一个 widthAnchor 约束,然后再将它们添加到堆栈视图中。
label.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 20).isActive = true
推荐阅读
- python - 将 pandas 数据框导出到 excel 时,小数四舍五入和 '%' 符号消失
- python-3.x - Python 保存到 .txt
- python - 使用 api4jenkins 在 Jenkins 中获取最新构建的控制台输出
- python - 为组中的所有行保持相同的众数值
- javascript - 使用 jquery 双重确认 SweetAlert
- apache - Htaccess 重写 ssid 哈希以防止匹配错误
- django - Django:创建扁平化 JSON 的临时表或视图
- powershell - VS Code - Powershell 模块 - 命令无法识别
- java - 与 Kotlin 的 take() 方法等效的 Java 是什么?
- excel - 有没有办法在 VB.Net 上使用 OleDbDataAdapter 提取 excel 的文化?