如何根据文本视图内容大小在滚动视图中设置启用或禁用滚动?
how to set enabled or disabled scrolling in scroll view based on the text view content size?
我正在尝试制作一个具有滚动视图和文本视图的视图控制器。我希望仅当文本视图的文本内容很多时才启用滚动。因为即使文本视图中的文本内容不太像下图,如果我可以滚动我的视图控制器看起来很难看
正如我们所见,视图控制器的底部有一个很大的缺口。这是我为此使用的代码 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()
}
}
根据文本视图contentSize
,调整滚动视图的可滚动属性。
在textview上设置文本后,检查如下:-
let height = self.notificationContentTextView.contentSize.height
if(height > scrollView.frame.size.height){
scrollView.isScrollEnabled == true
}else{
scrollView.isScrollEnabled == false
}
首先您需要找到 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) )
并设置文本视图的框架
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)
希望对您有所帮助!编码愉快 :)
我正在尝试制作一个具有滚动视图和文本视图的视图控制器。我希望仅当文本视图的文本内容很多时才启用滚动。因为即使文本视图中的文本内容不太像下图,如果我可以滚动我的视图控制器看起来很难看
正如我们所见,视图控制器的底部有一个很大的缺口。这是我为此使用的代码 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()
}
}
根据文本视图contentSize
,调整滚动视图的可滚动属性。
在textview上设置文本后,检查如下:-
let height = self.notificationContentTextView.contentSize.height
if(height > scrollView.frame.size.height){
scrollView.isScrollEnabled == true
}else{
scrollView.isScrollEnabled == false
}
首先您需要找到 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) )
并设置文本视图的框架
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)
希望对您有所帮助!编码愉快 :)