Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func alignementRange() {
- let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
- let selectedRange = NSRange(location: 63, length: 23)
- let leftParagraph: NSParagraphStyle = {
- let paragraph = NSMutableParagraphStyle()
- paragraph.alignment = .left
- return paragraph
- }()
- let rightParagraph: NSParagraphStyle = {
- let paragraph = NSMutableParagraphStyle()
- paragraph.alignment = .right
- return paragraph
- }()
- let centerParagraph: NSParagraphStyle = {
- let paragraph = NSMutableParagraphStyle()
- paragraph.alignment = .center
- return paragraph
- }()
- let justifiedParagraph: NSParagraphStyle = {
- let paragraph = NSMutableParagraphStyle()
- paragraph.alignment = .justified
- return paragraph
- }()
- let attributedString = NSMutableAttributedString(string: "Gee, Brain, what do you want to do tonight?")
- attributedString.append(NSAttributedString(string: "\nThe same thing we do every night, Pinky. Try to take over the world", attributes: [.paragraphStyle: leftParagraph]))
- attributedString.append(NSAttributedString(string: "\nThey're Pinky and The Brain", attributes: [.paragraphStyle: rightParagraph]))
- attributedString.append(NSAttributedString(string: "\nYes, Pinky and The Brain", attributes: [.paragraphStyle: centerParagraph]))
- attributedString.append(NSAttributedString(string: "\nOne is a genius", attributes: [.paragraphStyle: justifiedParagraph]))
- attributedString.append(NSAttributedString(string: "\nThe other's insane.", attributes: [.paragraphStyle: centerParagraph]))
- attributedString.append(NSAttributedString(string: "\nThey're laboratory mice", attributes: [.paragraphStyle: centerParagraph]))
- attributedString.append(NSAttributedString(string: "\nTheir genes have been spliced", attributes: [.paragraphStyle: centerParagraph]))
- attributedString.append(NSAttributedString(string: "\nThey're dinky"))
- attributedString.append(NSAttributedString(string: "\nThey're Pinky and The Brain, Brain, Brain, Brain"))
- attributedString.append(NSAttributedString(string: "\nBrain, Brain, Brain, Brain"))
- attributedString.append(NSAttributedString(string: "\nBrain"))
- attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: selectedRange)
- let otherSelectedRange = NSRange(location: 201, length: 23)
- attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: otherSelectedRange)
- let lastSelectedRange = NSRange(location: 317, length: 25)
- attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: lastSelectedRange)
- textView.attributedText = attributedString
- textView
- print(attributedString)
- func paragraphSearchRange(in attributedString: NSAttributedString, with initialRange: NSRange) -> NSRange {
- let previousNewLine = (attributedString.string as NSString).rangeOfCharacter(from: .newlines, options: .backwards, range: NSRange(location: 0, length: initialRange.location))
- let searchLocation = previousNewLine.location != NSNotFound ? previousNewLine.location : 0
- let nextNewLine = (attributedString.string as NSString).rangeOfCharacter(from: .newlines,
- range: NSRange(location: initialRange.location + initialRange.length,
- length: attributedString.length - initialRange.location - initialRange.length))
- let nextNewLineLocation = nextNewLine.location != NSNotFound ? nextNewLine.location : attributedString.length
- let searchLength = nextNewLineLocation - searchLocation
- let searchRange = NSRange(location: searchLocation, length: searchLength)
- return searchRange
- }
- let searchRange = paragraphSearchRange(in: attributedString, with: selectedRange)
- attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: searchRange)
- textView.attributedText = attributedString
- textView
- attributedString.enumerateAttribute(.paragraphStyle, in: searchRange) { value, subrange, _ in
- let substr = (attributedString.string as NSString).substring(with: subrange)
- print("Substr: \(substr)")
- }
- textView.attributedText = attributedString
- textView
- let otherSearchRange = paragraphSearchRange(in: attributedString, with: otherSelectedRange)
- attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: otherSearchRange)
- textView.attributedText = attributedString
- textView
- attributedString.enumerateAttribute(.paragraphStyle, in: otherSearchRange) { value, subrange, _ in
- // let substr = (attributedString.string as NSString).substring(with: subrange)
- // print("Substr: \(substr)")
- guard let style = value as? NSParagraphStyle, let newStyle = style.mutableCopy() as? NSMutableParagraphStyle else { return }
- newStyle.alignment = .left
- attributedString.addAttribute(.paragraphStyle, value: newStyle, range: subrange)
- }
- textView.attributedText = attributedString
- textView
- let lastSearchRange = paragraphSearchRange(in: attributedString, with: lastSelectedRange)
- attributedString.addAttribute(.backgroundColor, value: UIColor.blue, range: lastSearchRange)
- textView.attributedText = attributedString
- textView
- attributedString.enumerateAttribute(.paragraphStyle, in: lastSearchRange) { value, subrange, _ in
- // let substr = (attributedString.string as NSString).substring(with: subrange)
- // print("Substr: \(substr)")
- let newStyle: NSMutableParagraphStyle
- if let currentStyle = value as? NSParagraphStyle, let copyStyle = currentStyle.mutableCopy() as? NSMutableParagraphStyle {
- newStyle = copyStyle
- } else {
- newStyle = NSMutableParagraphStyle()
- }
- newStyle.alignment = .center
- attributedString.addAttribute(.paragraphStyle, value: newStyle, range: subrange)
- }
- textView.attributedText = attributedString
- textView
- }
- alignementRange()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement