Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve breakLine handling #45

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
}

func visit(_ markup: BreakLineMarkup) -> Result {
return makeBreakLine(in: markup)
return makeBreakLine(in: markup, reduceable: false)
}

func visit(_ markup: RawStringMarkup) -> Result {
Expand Down Expand Up @@ -95,7 +95,9 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
if paragraphSpacingPolicy == .lineBreaks {
attributedString.append(makeBreakLine(in: markup, reduceable: false))
}
attributedString.insert(makeBreakLine(in: markup, reduceable: false), at: 0)
if markup.parentMarkup?.childMarkups.first !== markup { // only add a prefix breakLine if there are other elements before it
attributedString.insert(makeBreakLine(in: markup, reduceable: false), at: 0)
}
return attributedString
}

Expand Down Expand Up @@ -188,22 +190,34 @@ struct MarkupNSAttributedStringVisitor: MarkupVisitor {
extension MarkupNSAttributedStringVisitor {
// Find continues reduceable breakline and merge it.
// e.g. Test\n\n\n\nTest -> Test\nTest
// e.g. \nTest -> Test
// e.g. Test\n -> Test
func reduceBreaklineInResultNSAttributedString(_ attributedString: NSAttributedString) -> NSAttributedString {
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)

let totalLength = mutableAttributedString.string.utf16.count
mutableAttributedString.enumerateAttribute(.reduceableBreakLine, in: NSMakeRange(0, totalLength)) { reduceableBreakLine, range, _ in
if let reduceableBreakLine = reduceableBreakLine as? Bool, reduceableBreakLine == true {
if range.location == 0 || range.upperBound == mutableAttributedString.string.utf16.count {
// remove prefix & suffix break line
mutableAttributedString.replaceCharacters(in: range, with: "")
} else if range.length >= 2 {
// remove reduntant
mutableAttributedString.replaceCharacters(in: range, with: "\n")
if totalLength > 0 {
mutableAttributedString.enumerateAttribute(.reduceableBreakLine, in: NSMakeRange(0, totalLength)) { reduceableBreakLine, range, _ in
if let reduceableBreakLine = reduceableBreakLine as? Bool, reduceableBreakLine == true {
if range.length >= 2 {
// remove redundant
mutableAttributedString.replaceCharacters(in: range, with: "\n")
}
}
}

//prefix break line:
var range = NSRange()
if mutableAttributedString.attribute(.isBreakLine, at: 0, effectiveRange: &range) != nil {
mutableAttributedString.replaceCharacters(in: range, with: "")
}

//suffix break line:
if mutableAttributedString.attribute(.isBreakLine, at: mutableAttributedString.string.utf16.count - 1, effectiveRange: &range) != nil {
mutableAttributedString.replaceCharacters(in: range, with: "")
}
}

return mutableAttributedString
}

Expand Down Expand Up @@ -293,3 +307,6 @@ private extension MarkupNSAttributedStringVisitor {
private extension NSAttributedString.Key {
static let reduceableBreakLine: NSAttributedString.Key = .init("reduceableBreakLine")
}
private extension NSAttributedString.Key {
static let isBreakLine: NSAttributedString.Key = .init("isBreakLine")
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ final class MarkupNSAttributedStringVisitorTests: XCTestCase {
private extension NSAttributedString.Key {
static let reduceableBreakLine: NSAttributedString.Key = .init("reduceableBreakLine")
}
private extension NSAttributedString.Key {
static let isBreakLine: NSAttributedString.Key = .init("isBreakLine")
}
Loading