at_nil

String boundingRect

Feb 12th, 2020
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.90 KB | None | 0 0
  1. //: A UIKit based Playground for presenting user interface
  2.  
  3. import UIKit
  4. import PlaygroundSupport
  5.  
  6. class MyViewController : UIViewController {
  7.     override func loadView() {
  8.         let view = UIView()
  9.         view.backgroundColor = .white
  10.         self.view = view
  11.  
  12.         let string = """
  13.        Мальчики и девочки🙋‍♂💥💁🏼‍♀
  14.        Появилась одна 🙀🙀 вакансия iOS-Разработчика ВКонтакте🤴💰🇷🇺 собеседования уже в феврале 2⃣0⃣2⃣0⃣
  15.        Записывайтесь, спешите😉
  16.        Вопросы в ЛС💃💃💃  vk.com/nil
  17.        """
  18.         let paragraphStyle = NSMutableParagraphStyle()
  19.         paragraphStyle.lineBreakMode = .byWordWrapping
  20.         let attributtes = [
  21.             NSAttributedString.Key.font : UIFont.systemFont(ofSize: UIFont.systemFontSize),
  22.             NSAttributedString.Key.foregroundColor: UIColor.black,
  23.             NSAttributedString.Key.paragraphStyle: paragraphStyle,
  24.         ]
  25.         let attributedString = NSAttributedString(string: string, attributes: attributtes)
  26.        
  27.         let maxSize = CGSize(width: 200, height: CGFloat.greatestFiniteMagnitude)
  28.        
  29.         let boundingRect = attributedString.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], context: nil)
  30.         let boundingSize = boundingRect.size
  31.         UIGraphicsBeginImageContextWithOptions(boundingSize, false, UIScreen.main.scale)
  32.         attributedString.draw(in: boundingRect)
  33.         let image = UIGraphicsGetImageFromCurrentImageContext()
  34.         UIGraphicsEndImageContext()
  35.        
  36.         let xPadding: CGFloat = 10
  37.        
  38.         let imageView = UIImageView(frame: CGRect(x: xPadding, y: 100, width: boundingSize.width, height: boundingSize.height))
  39.         imageView.image = image
  40.         view.addSubview(imageView)
  41.        
  42.         let label = UILabel()
  43.         label.numberOfLines = 0
  44.         label.attributedText = attributedString
  45.         let labelFittingSize = label.sizeThatFits(maxSize)
  46.         label.frame = CGRect(x: xPadding, y: imageView.frame.maxY + 10, width: labelFittingSize.width, height: labelFittingSize.height)
  47.         view.addSubview(label)
  48.        
  49.         let textView = UITextView()
  50.         textView.attributedText = attributedString
  51.         let textViewFittingSize = textView.sizeThatFits(maxSize)
  52.         textView.frame = CGRect(x: xPadding, y: label.frame.maxY + 10, width: textViewFittingSize.width, height: textViewFittingSize.height)
  53.         view.addSubview(textView)
  54.        
  55.         print("String bounding size:" + boundingSize.debugDescription + " UILabel fitting size:" + labelFittingSize.debugDescription + " UITextView fitting size:" + textViewFittingSize.debugDescription)
  56.     }
  57. }
  58. // Present the view controller in the Live View window
  59. PlaygroundPage.current.liveView = MyViewController()
Advertisement
Add Comment
Please, Sign In to add comment