Advertisement
Don_Mag

Word Wrap Test

Jun 22nd, 2022
1,726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.03 KB | None | 0 0
  1. class TextViewLabel: UITextView {
  2.    
  3.     override init(frame: CGRect, textContainer: NSTextContainer?) {
  4.         super.init(frame: frame, textContainer: textContainer)
  5.         commonInit()
  6.     }
  7.    
  8.     required init?(coder aDecoder: NSCoder) {
  9.         super.init(coder: aDecoder)
  10.         commonInit()
  11.     }
  12.    
  13.     override func prepareForInterfaceBuilder() {
  14.         super.prepareForInterfaceBuilder()
  15.         commonInit()
  16.     }
  17.     func commonInit() -> Void {
  18.         isScrollEnabled = false
  19.         isEditable = false
  20.         isSelectable = false
  21.         textContainerInset = UIEdgeInsets.zero
  22.         textContainer.lineFragmentPadding = 0
  23.     }
  24.    
  25. }
  26.  
  27. class TextViewLabelTestVC: UIViewController {
  28.  
  29.     override func viewDidLoad() {
  30.         super.viewDidLoad()
  31.  
  32.         let stack: UIStackView = {
  33.             let v = UIStackView()
  34.             v.axis = .vertical
  35.             v.spacing = 8
  36.             v.translatesAutoresizingMaskIntoConstraints = false
  37.             return v
  38.         }()
  39.  
  40.         let v1 = UILabel()
  41.         let v2 = TextViewLabel()
  42.         let v3 = UILabel()
  43.         let v4 = TextViewLabel()
  44.  
  45.         [v1, v2, v3, v4].forEach { v in
  46.             let lb = UILabel()
  47.             lb.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
  48.             if let v = v as? UILabel {
  49.                 lb.text = "UILabel"
  50.                 v.backgroundColor = .cyan
  51.                 v.font = .systemFont(ofSize: 16.0)
  52.                 v.numberOfLines = 0
  53.             }
  54.             if let v = v as? TextViewLabel {
  55.                 lb.text = "Text View Label"
  56.                 v.backgroundColor = .green
  57.                 v.font = .systemFont(ofSize: 16.0)
  58.             }
  59.             stack.addArrangedSubview(lb)
  60.             stack.addArrangedSubview(v)
  61.         }
  62.        
  63.         stack.setCustomSpacing(32.0, after: v2)
  64.        
  65.         view.addSubview(stack)
  66.        
  67.         let g = view.safeAreaLayoutGuide
  68.         NSLayoutConstraint.activate([
  69.        
  70.             stack.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  71.             stack.widthAnchor.constraint(equalToConstant: 255.0),
  72.             stack.centerXAnchor.constraint(equalTo: g.centerXAnchor),
  73.  
  74.         ])
  75.        
  76.         var vs: String = "Does this string result in an orphan?"
  77.  
  78.         v1.text = vs
  79.         v2.text = vs
  80.        
  81.         vs = "This string should wrap onto four lines of text. Compare the word wrap to see if TextKit allows the orphan."
  82.  
  83.         v3.text = vs
  84.         v4.text = vs
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement