Created
August 19, 2021 14:55
-
-
Save joshkopecek/11e3f27f0969b4155ccbe0562e5c7cd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RTLParser.swift | |
// | |
// Created by Josh Kopecek on 19/08/2021. | |
// | |
import Foundation | |
import MarkdownKit | |
class MarkdownRTL: MarkdownElement { | |
var regex: String = "(.+)" | |
func regularExpression() throws -> NSRegularExpression { | |
return try NSRegularExpression(pattern: regex, options: []) | |
} | |
func match(_ match: NSTextCheckingResult, | |
attributedString: NSMutableAttributedString) { | |
let matchAttr = attributedString.attributedSubstring(from: match.range) | |
let firstCharAsUnicodeScalar = matchAttr.string.unicodeScalars.first?.value | |
if firstCharAsUnicodeScalar != nil && firstCharAsUnicodeScalar! >= 1424 && firstCharAsUnicodeScalar! <= 1983 { | |
let rightAlign = NSMutableParagraphStyle() | |
rightAlign.alignment = .right | |
attributedString.addAttributes([.paragraphStyle: rightAlign], range: match.range) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment