Reputation: 13803
I am trying to make a view controller that has a scroll view and text view. I want my scrolling is enabled only when the text content of the text view is a lot. because it seems ugly if I can scroll my view controller even though the text content in the text view is not to much like the picture below
as we can see there is a big gap to the bottom of view controller. this is the code I use for this 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()
}
}
Upvotes: 0
Views: 689
Reputation: 127
First you need to find size of text of your textview, you can find size of text using :
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
}
after that set size of your textview according to your text size :
let contentSize : CGSize = rectForText(text: str, font:UIFont.boldSystemFont(ofSize: 15.0) , maxSize : CGSize(width: 200 * (SCREEN_WIDTH / 320), height: 20000) )
and set frame of your textview
var frame = self.myTextView.frame
frame.size.height = contentSize.height
self.myTextView.frame = frame
after set set contentsize of your scrollview
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: myTextView.frame.size.height + 8)
Hope this help! Happy coding :)
Upvotes: 3
Reputation: 8504
Depending upon the contentSize
of text view, adjust the scrollable property of scroll view.
After setting the text on textview, check as below:-
let height = self.notificationContentTextView.contentSize.height
if(height > scrollView.frame.size.height){
scrollView.isScrollEnabled == true
}else{
scrollView.isScrollEnabled == false
}
Upvotes: -2