Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StrTestVC: UIViewController, UITextViewDelegate {
- var textView: ScriptEditingView!
- override func viewDidLoad() {
- super.viewDidLoad()
- textView = ScriptEditingView(frame: .zero, textContainer: nil)
- textView.backgroundColor = .black
- textView.delegate = self
- view.addSubview(textView)
- textView.allowsEditingTextAttributes = true
- let guide = view.safeAreaLayoutGuide
- textView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- textView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
- textView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
- textView.topAnchor.constraint(equalTo: view.topAnchor),
- textView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
- ])
- let attributedString = sampleAttrString()
- textView.attributedText = attributedString
- NSLog("Attributed now")
- dumpAttributesOfText()
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- NSLog("Attributes after 1 sec")
- self.dumpAttributesOfText()
- }
- }
- private func dumpAttributesOfText() {
- textView.attributedText?.enumerateAttributes(in: NSRange(location: 0, length: textView.attributedText!.length), options: .longestEffectiveRangeNotRequired, using: { dictionary, range, stop in
- NSLog(" range \(range)")
- if let font = dictionary[.font] as? UIFont {
- NSLog("Font at range \(range) - \(font.fontName), \(font.pointSize)")
- }
- if let foregroundColor = dictionary[.foregroundColor] as? UIColor {
- NSLog("Foregroundcolor \(foregroundColor) at range \(range)")
- }
- if let underline = dictionary[.underlineStyle] as? Int {
- NSLog("Underline \(underline) at range \(range)")
- }
- })
- }
- func sampleAttrString() -> NSMutableAttributedString {
- guard let theFont: UIFont = UIFont(name: "HelveticaNeue", size: 30.0) else {
- fatalError()
- }
- let attsA: [NSAttributedString.Key : Any] = [
- .font: theFont,
- .foregroundColor: UIColor.white,
- ]
- let gRGB = CGColorSpace(name: CGColorSpace.genericRGBLinear)!
- let theCGColor = CGColor(colorSpace: gRGB, components: [0.96863, 0.80784, 0.27451, 1])!
- let theColor = UIColor(cgColor: theCGColor)
- let attsB: [NSAttributedString.Key : Any] = [
- .font: theFont,
- .foregroundColor: theColor,
- ]
- let partOne = NSMutableAttributedString(string: "1234567890123 ", attributes: attsA)
- let partTwo = NSAttributedString(string: "String", attributes: attsB)
- partOne.append(partTwo)
- return partOne
- }
- }
- /*
- output:
- Attributed now
- range {0, 14}
- Font at range {0, 14} - HelveticaNeue, 30.0
- Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 14}
- range {14, 6}
- Font at range {14, 6} - HelveticaNeue, 30.0
- Foregroundcolor kCGColorSpaceModelRGB 0.96863 0.80784 0.27451 1 at range {14, 6}
- Attributes after 1 sec
- range {0, 14}
- Font at range {0, 14} - HelveticaNeue, 30.0
- Foregroundcolor UIExtendedGrayColorSpace 1 1 at range {0, 14}
- range {14, 6}
- Font at range {14, 6} - HelveticaNeue, 30.0
- Foregroundcolor kCGColorSpaceModelRGB 0.96863 0.80784 0.27451 1 at range {14, 6}
- */
Add Comment
Please, Sign In to add comment