如何根据文本视图内容大小设置滚动视图中的启用或禁用滚动?

时间:2018-03-16 10:18:16

标签: ios swift uiscrollview uitextview

enter image description here

我正在尝试创建一个具有滚动视图和文本视图的视图控制器。我希望仅当文本视图的文本内容很多时才启用滚动。因为如果我可以滚动我的视图控制器看起来很难看,即使文本视图中的文本内容与下图不太相似

enter image description here

我们可以看到视图控制器底部存在很大差距。这是我用于此VC的代码

import UIKit

class NotificationDetailVC: UIViewController {

    @IBOutlet weak var notificationTitleLabel: UILabel!
    @IBOutlet weak var notificationImage: UIImageView!
    @IBOutlet weak var notificationDateLabel: UILabel!
    @IBOutlet weak var notificationContentTextView: UITextView!

    var dataNotification : [String:Any]?

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
        updateReadStatusInUserDefault()
    }

    @IBAction func imageDidTapped(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let popup = storyboard.instantiateViewController(withIdentifier: "imageDisplay") as! ReusableImageDisplayVC
        popup.photo = notificationImage.image
        self.present(popup, animated: true, completion: nil)
    }

    func updateUI() {
        guard let dataNotification = dataNotification else {return}
        notificationTitleLabel.text = dataNotification["Judul"] as? String
        notificationContentTextView.text = dataNotification["Keterangan"] as? String

        guard let notifDate = dataNotification["TglNotif"] as? String else {return}
        notificationDateLabel.text = DateTimeService.changeFormat(of: notifDate, toFormat: "d MMM YYY")

        let imagePath = dataNotification["Photo"] as? String
        guard let encodedImagePath = imagePath?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {return}

        if imagePath != nil {
            if imagePath!.isEmpty {
                notificationImage.removeFromSuperview()
            } else {
                notificationImage.sd_setImage(with: URL(string: encodedImagePath), placeholderImage: UIImage(named: "3-2 Img"), options: [.continueInBackground, .progressiveDownload])
            }
        } else {
            notificationImage.removeFromSuperview()
        }
    }

    func updateReadStatusInUserDefault() {
        guard let dataNotification = dataNotification,
            let notifID = dataNotification["notif_id"] as? Int else {return}

        guard var readNotificationStatusUserDefault  = UserDefaults.standard.object(forKey: "readNotification") as? [String:String] else {return}

        readNotificationStatusUserDefault["\(notifID)"] = "1"
        UserDefaults.standard.set(readNotificationStatusUserDefault, forKey: "readNotification")
        UserDefaults.standard.synchronize()
    }
}

2 个答案:

答案 0 :(得分:3)

首先,您需要找到 textview 的文字大小, 您可以使用以下方式查找文本大小:

func rectForText(text: String, font: UIFont, maxSize: CGSize) -> CGSize
{
    let attrString = NSAttributedString.init(string: yourtext, attributes: [NSAttributedStringKey.font:font])
    let rect = attrString.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
    let size = CGSize(width: rect.size.width, height: rect.size.height)//(, )
    return size
}

根据您的文字大小设置 textview 的尺寸:

let contentSize : CGSize = rectForText(text: str, font:UIFont.boldSystemFont(ofSize: 15.0) , maxSize : CGSize(width: 200 * (SCREEN_WIDTH / 320), height: 20000) )

并设置textview的框架

var frame = self.myTextView.frame
frame.size.height = contentSize.height
self.myTextView.frame = frame

设置滚动视图的设置内容后

scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: myTextView.frame.size.height + 8)

希望这有帮助!快乐编码:)

答案 1 :(得分:-2)

根据文本视图的contentSize,调整滚动视图的可滚动属性。

在textview上设置文本后,请按以下方式检查: -

    let height = self.notificationContentTextView.contentSize.height
if(height > scrollView.frame.size.height){
scrollView.isScrollEnabled == true
}else{
scrollView.isScrollEnabled == false
}