【问题标题】:How to make UITextView adjust according to the length of text?如何使 UITextView 根据文本长度进行调整?
【发布时间】:2021-06-28 23:57:35
【问题描述】:

我有一个 ViewController,它有四个 UITextView 和四个 UILabel。我有他们每个人的文本,它们在一个数组中。我试图使 UITextViews 和 UILabels 在彼此之间具有相同长度的受限距离,无论字段的长度如何。每次我尝试对 ViewController 应用约束时,它都会弄乱上面的所有内容(标签、文本视图等)。 ViewController 有一个滚动视图。以下是截图和代码:

import UIKit

class DetailViewController: UIViewController {

    var name = ""
    var img: UIImage?
    var desc = ""
    var healingProp = ""
    var spiritualProp = ""
    var emotionalProp = ""
    
    @IBOutlet weak var detailCrystalPhotoImageView: UIImageView!
   // about the crystal
    @IBOutlet weak var crystalDescriptionTextView: UITextView!
    @IBOutlet weak var backgroundInfoLabel: UILabel!
    // healing properties
    @IBOutlet weak var healingPropertiesLabel: UILabel!
    @IBOutlet weak var healingPropertiesInfoTextView: UITextView!
    @IBOutlet weak var scrollView: UIScrollView!
   // spiritual properties
    @IBOutlet weak var spiritualPropertiesLabel: UILabel!
    @IBOutlet weak var spiritualPropertiesInfoTextView: UITextView!
    // emotional properties
    @IBOutlet weak var emotionalPropertiesLabel: UILabel!
    @IBOutlet weak var emotionalPropertiesInfoTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // sets up scroll view
        scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height+1000)
        
        // displays crystal title and crystal image
        title = name
        detailCrystalPhotoImageView.image = img
        
        // display background label and text
        backgroundInfoLabel.text = String("About " + name + " ℹ️")
        backgroundInfoLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
       
        // sets up crystal summary
        crystalDescriptionTextView.text = desc
        crystalDescriptionTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        
        // display healing properties label and text
        healingPropertiesLabel.text = String("Healing Properties ????")
        healingPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        healingPropertiesInfoTextView.text = healingProp
        healingPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        healingPropertiesInfoTextView.sizeToFit()
        
        // display spiritual properties label and text
        spiritualPropertiesLabel.text = String("Spiritual Properties ????️")
        spiritualPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        spiritualPropertiesInfoTextView.text = spiritualProp
        spiritualPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        
        //display emotional properties label and text
        emotionalPropertiesLabel.text = String("Emotional Properties ????")
        emotionalPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        emotionalPropertiesInfoTextView.text = emotionalProp
        emotionalPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        
        
    }
}

【问题讨论】:

  • 你的描述与你的问题标题无关!!!

标签: ios swift iphone xcode ipad


【解决方案1】:

子类是实现调整大小以适应功能的方法:

class DTextView: UITextView {

    @IBInspectable var maxHeight: CGFloat = 0
    @IBInspectable var enableAutoResizeToFitContent: Bool = false

    override func awakeFromNib() {
        super.awakeFromNib()
        
        NotificationCenter.default.addObserver(self, selector: #selector(DTextView.didChangeText(_:)), name:UITextView.textDidChangeNotification, object: self)
    }

    @objc func didChangeText(_ note: Notification) {
        // needed incase isScrollEnabled is set to true which stops automatically calling invalidateIntrinsicContentSize()
        invalidateIntrinsicContentSize()
    }

    override var intrinsicContentSize: CGSize {
        var size = super.intrinsicContentSize
        
        if size.height == UIView.noIntrinsicMetric {
            // force layout
            layoutManager.glyphRange(for: textContainer)
            size.height = layoutManager.usedRect(for: textContainer).height + textContainerInset.top + textContainerInset.bottom
        }
        
        if !enableAutoResizeToFitContent {
            if !isScrollEnabled {
                isScrollEnabled = true
            }
        } else {
            if maxHeight > 0.0 && size.height > maxHeight {
                size.height = maxHeight
            
                if !isScrollEnabled {
                    isScrollEnabled = true
                }
            } else if isScrollEnabled {
                isScrollEnabled = false
            }
        }
        
        return size
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

如果您将maxHeight 属性设置为ZeroTextView 将自动调整大小以适应内容大小。

应用的约束:

  1. 更大或相等的高度限制。 (最小高度:可能为零).
  2. 优先级低于Content Hugging Priority的高度限制。

两个约束应该具有相同的常数值(请调查)

【讨论】:

  • 让我检查一下约束应该是怎样的。
  • 欢迎提出任何问题。回来告诉我结果。总帐
  • 这是 DetailViewController 的子类吗?
  • 我将它设为 UITextView 的子类,但出现错误:Type 'DTextView' has no member 'didChangeText'
  • 它是 UITextView 的子类。 didChangeText 是一个自定义函数。我修改了答案并添加了它。立即检查。
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
相关资源
最近更新 更多