Advertisement
Larme

Untitled

Mar 2nd, 2021
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.16 KB | None | 0 0
  1. func alignementRange() {
  2.  
  3.     let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
  4.     let selectedRange = NSRange(location: 63, length: 23)
  5.  
  6.     let leftParagraph: NSParagraphStyle = {
  7.         let paragraph = NSMutableParagraphStyle()
  8.         paragraph.alignment = .left
  9.         return paragraph
  10.     }()
  11.     let rightParagraph: NSParagraphStyle = {
  12.         let paragraph = NSMutableParagraphStyle()
  13.         paragraph.alignment = .right
  14.         return paragraph
  15.     }()
  16.     let centerParagraph: NSParagraphStyle = {
  17.         let paragraph = NSMutableParagraphStyle()
  18.         paragraph.alignment = .center
  19.         return paragraph
  20.     }()
  21.     let justifiedParagraph: NSParagraphStyle = {
  22.         let paragraph = NSMutableParagraphStyle()
  23.         paragraph.alignment = .justified
  24.         return paragraph
  25.     }()
  26.  
  27.     let attributedString = NSMutableAttributedString(string: "Gee, Brain, what do you want to do tonight?")
  28.     attributedString.append(NSAttributedString(string: "\nThe same thing we do every night, Pinky. Try to take over the world", attributes: [.paragraphStyle: leftParagraph]))
  29.     attributedString.append(NSAttributedString(string: "\nThey're Pinky and The Brain", attributes: [.paragraphStyle: rightParagraph]))
  30.     attributedString.append(NSAttributedString(string: "\nYes, Pinky and The Brain", attributes: [.paragraphStyle: centerParagraph]))
  31.     attributedString.append(NSAttributedString(string: "\nOne is a genius", attributes: [.paragraphStyle: justifiedParagraph]))
  32.     attributedString.append(NSAttributedString(string: "\nThe other's insane.", attributes: [.paragraphStyle: centerParagraph]))
  33.     attributedString.append(NSAttributedString(string: "\nThey're laboratory mice", attributes: [.paragraphStyle: centerParagraph]))
  34.     attributedString.append(NSAttributedString(string: "\nTheir genes have been spliced", attributes: [.paragraphStyle: centerParagraph]))
  35.     attributedString.append(NSAttributedString(string: "\nThey're dinky"))
  36.     attributedString.append(NSAttributedString(string: "\nThey're Pinky and The Brain, Brain, Brain, Brain"))
  37.     attributedString.append(NSAttributedString(string: "\nBrain, Brain, Brain, Brain"))
  38.     attributedString.append(NSAttributedString(string: "\nBrain"))
  39.  
  40.     attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: selectedRange)
  41.  
  42.  
  43.     let otherSelectedRange = NSRange(location: 201, length: 23)
  44.     attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: otherSelectedRange)
  45.  
  46.     let lastSelectedRange = NSRange(location: 317, length: 25)
  47.     attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: lastSelectedRange)
  48.  
  49.     textView.attributedText = attributedString
  50.  
  51.     textView
  52.  
  53.     print(attributedString)
  54.  
  55.     func paragraphSearchRange(in attributedString: NSAttributedString, with initialRange: NSRange) -> NSRange {
  56.         let previousNewLine = (attributedString.string as NSString).rangeOfCharacter(from: .newlines, options: .backwards, range: NSRange(location: 0, length: initialRange.location))
  57.         let searchLocation = previousNewLine.location != NSNotFound ? previousNewLine.location : 0
  58.         let nextNewLine = (attributedString.string as NSString).rangeOfCharacter(from: .newlines,
  59.                                                                                  range: NSRange(location: initialRange.location + initialRange.length,
  60.                                                                                                 length: attributedString.length - initialRange.location - initialRange.length))
  61.         let nextNewLineLocation = nextNewLine.location != NSNotFound ? nextNewLine.location : attributedString.length
  62.         let searchLength = nextNewLineLocation - searchLocation
  63.         let searchRange = NSRange(location: searchLocation, length: searchLength)
  64.         return searchRange
  65.     }
  66.  
  67.     let searchRange = paragraphSearchRange(in: attributedString, with: selectedRange)
  68.     attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: searchRange)
  69.     textView.attributedText = attributedString
  70.     textView
  71.  
  72.     attributedString.enumerateAttribute(.paragraphStyle, in: searchRange) { value, subrange, _ in
  73.         let substr = (attributedString.string as NSString).substring(with: subrange)
  74.         print("Substr: \(substr)")
  75.     }
  76.     textView.attributedText = attributedString
  77.     textView
  78.  
  79.     let otherSearchRange = paragraphSearchRange(in: attributedString, with: otherSelectedRange)
  80.     attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: otherSearchRange)
  81.     textView.attributedText = attributedString
  82.     textView
  83.  
  84.     attributedString.enumerateAttribute(.paragraphStyle, in: otherSearchRange) { value, subrange, _ in
  85. //        let substr = (attributedString.string as NSString).substring(with: subrange)
  86. //        print("Substr: \(substr)")
  87.         guard let style = value as? NSParagraphStyle, let newStyle = style.mutableCopy() as? NSMutableParagraphStyle else { return }
  88.         newStyle.alignment = .left
  89.         attributedString.addAttribute(.paragraphStyle, value: newStyle, range: subrange)
  90.     }
  91.     textView.attributedText = attributedString
  92.     textView
  93.  
  94.  
  95.     let lastSearchRange = paragraphSearchRange(in: attributedString, with: lastSelectedRange)
  96.     attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: lastSearchRange)
  97.     textView.attributedText = attributedString
  98.     textView
  99.  
  100.     attributedString.enumerateAttribute(.paragraphStyle, in: lastSearchRange) { value, subrange, _ in
  101. //        let substr = (attributedString.string as NSString).substring(with: subrange)
  102. //        print("Substr: \(substr)")
  103.         let newStyle: NSMutableParagraphStyle
  104.         if let currentStyle = value as? NSParagraphStyle, let copyStyle = currentStyle.mutableCopy() as? NSMutableParagraphStyle {
  105.             newStyle = copyStyle
  106.         } else {
  107.             newStyle = NSMutableParagraphStyle()
  108.         }
  109.         newStyle.alignment = .center
  110.         attributedString.addAttribute(.paragraphStyle, value: newStyle, range: subrange)
  111.     }
  112.     textView.attributedText = attributedString
  113.     textView
  114. }
  115.  
  116. alignementRange()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement