Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ThirdViewController: UIViewController {
- @IBOutlet weak var imageView: UIImageView!
- @IBOutlet weak var myTextView: UITextView!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
- // Combine the new text with the old
- let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
- // Create attributed version of the text
- let attributedText = NSMutableAttributedString(string: combinedText)
- attributedText.addAttribute(NSFontAttributeName, value: textView.font, range: NSMakeRange(0, attributedText.length))
- // Get the padding of the text container
- let padding = textView.textContainer.lineFragmentPadding
- // Create a bounding rect size by subtracting the padding
- // from both sides and allowing for unlimited length
- let boundingSize = CGSizeMake(textView.frame.size.width - padding * 2, CGFloat.max)
- // Get the bounding rect of the attributed text in the
- // given frame
- let boundingRect = attributedText.boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
- // Compare the boundingRect plus the top and bottom padding
- // to the text view height; if the new bounding height would be
- // less than or equal to the text view height, append the text
- if (boundingRect.size.height + padding * 2 <= textView.frame.size.height){
- return true
- }
- else {
- return false
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement