Don_Mag

Untitled

Jul 2nd, 2022 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.12 KB | None | 0 0
  1. class StrTestVC: UIViewController, UITextViewDelegate {
  2.    
  3.     var textView: ScriptEditingView!
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         textView = ScriptEditingView(frame: .zero, textContainer: nil)
  9.         textView.backgroundColor = .black
  10.         textView.delegate = self
  11.         view.addSubview(textView)
  12.         textView.allowsEditingTextAttributes = true
  13.        
  14.         let guide = view.safeAreaLayoutGuide
  15.        
  16.         textView.translatesAutoresizingMaskIntoConstraints = false
  17.         NSLayoutConstraint.activate([
  18.             textView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
  19.             textView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
  20.             textView.topAnchor.constraint(equalTo: view.topAnchor),
  21.             textView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  22.         ])
  23.        
  24.         let attributedString = sampleAttrString()
  25.  
  26.         textView.attributedText = attributedString
  27.        
  28.         NSLog("Attributed now")
  29.         dumpAttributesOfText()
  30.        
  31.         DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  32.             NSLog("Attributes after 1 sec")
  33.             self.dumpAttributesOfText()
  34.         }
  35.     }
  36.  
  37.     private func dumpAttributesOfText() {
  38.         textView.attributedText?.enumerateAttributes(in: NSRange(location: 0, length: textView.attributedText!.length), options: .longestEffectiveRangeNotRequired, using: { dictionary, range, stop in
  39.             NSLog(" range \(range)")
  40.            
  41.             if let font = dictionary[.font] as? UIFont {
  42.                 NSLog("Font at range \(range) - \(font.fontName), \(font.pointSize)")
  43.             }
  44.            
  45.             if let foregroundColor = dictionary[.foregroundColor] as? UIColor {
  46.                 NSLog("Foregroundcolor \(foregroundColor) at range \(range)")
  47.             }
  48.            
  49.             if let underline = dictionary[.underlineStyle] as? Int {
  50.                 NSLog("Underline \(underline) at range \(range)")
  51.             }
  52.         })
  53.     }
  54.    
  55.     func sampleAttrString() -> NSMutableAttributedString {
  56.         guard let theFont: UIFont = UIFont(name: "HelveticaNeue", size: 30.0) else {
  57.             fatalError()
  58.         }
  59.        
  60.         let attsA: [NSAttributedString.Key : Any] = [
  61.             .font: theFont,
  62.             .foregroundColor: UIColor.white,
  63.         ]
  64.        
  65.         let gRGB = CGColorSpace(name: CGColorSpace.genericRGBLinear)!
  66.         let theCGColor = CGColor(colorSpace: gRGB, components: [0.96863, 0.80784, 0.27451, 1])!
  67.         let theColor = UIColor(cgColor: theCGColor)
  68.  
  69.         let attsB: [NSAttributedString.Key : Any] = [
  70.             .font: theFont,
  71.             .foregroundColor: theColor,
  72.         ]
  73.        
  74.         let partOne = NSMutableAttributedString(string: "1234567890123 ", attributes: attsA)
  75.         let partTwo = NSAttributedString(string: "String", attributes: attsB)
  76.        
  77.         partOne.append(partTwo)
  78.        
  79.         return partOne
  80.     }
  81.  
  82. }
  83.  
  84. /*
  85. output:
  86.  
  87.  Attributed now
  88.   range {0, 14}
  89.  Font at range {0, 14} - HelveticaNeue, 30.0
  90.  Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 14}
  91.   range {14, 6}
  92.  Font at range {14, 6} - HelveticaNeue, 30.0
  93.  Foregroundcolor kCGColorSpaceModelRGB 0.96863 0.80784 0.27451 1  at range {14, 6}
  94.  
  95.  Attributes after 1 sec
  96.   range {0, 14}
  97.  Font at range {0, 14} - HelveticaNeue, 30.0
  98.  Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 14}
  99.   range {14, 6}
  100.  Font at range {14, 6} - HelveticaNeue, 30.0
  101.  Foregroundcolor kCGColorSpaceModelRGB 0.96863 0.80784 0.27451 1  at range {14, 6}
  102.  
  103. */
  104.  
Add Comment
Please, Sign In to add comment