Skip to content
Merged
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
8 changes: 8 additions & 0 deletions Aztec.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
B57D1C3D1E92C38000EA4B16 /* HTMLAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B57D1C3C1E92C38000EA4B16 /* HTMLAttachment.swift */; };
B59C9F9F1DF74BB80073B1D6 /* UIFont+Traits.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59C9F9E1DF74BB80073B1D6 /* UIFont+Traits.swift */; };
B5A99D841EBA073D00DED081 /* HTMLStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5A99D831EBA073D00DED081 /* HTMLStorage.swift */; };
B5AB79F81F5F3E0B00DF26F5 /* String+EndOfParagraph.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5AB79F71F5F3E0B00DF26F5 /* String+EndOfParagraph.swift */; };
B5AB79FA1F5F403C00DF26F5 /* StringEndOfParagraphTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5AB79F91F5F403C00DF26F5 /* StringEndOfParagraphTests.swift */; };
B5AF89321E93CFC80051EFDB /* RenderableAttachmentDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5AF89311E93CFC80051EFDB /* RenderableAttachmentDelegate.swift */; };
B5B86D371DA3EC250083DB3F /* NSRange+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F18733C41DA096EE005AEB80 /* NSRange+Helpers.swift */; };
B5B96DAB1E01B2F300791315 /* UIPasteboard+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5B96DAA1E01B2F300791315 /* UIPasteboard+Helpers.swift */; };
Expand Down Expand Up @@ -219,6 +221,8 @@
B57D1C3C1E92C38000EA4B16 /* HTMLAttachment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLAttachment.swift; sourceTree = "<group>"; };
B59C9F9E1DF74BB80073B1D6 /* UIFont+Traits.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIFont+Traits.swift"; sourceTree = "<group>"; };
B5A99D831EBA073D00DED081 /* HTMLStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTMLStorage.swift; sourceTree = "<group>"; };
B5AB79F71F5F3E0B00DF26F5 /* String+EndOfParagraph.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+EndOfParagraph.swift"; sourceTree = "<group>"; };
B5AB79F91F5F403C00DF26F5 /* StringEndOfParagraphTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StringEndOfParagraphTests.swift; path = Extensions/StringEndOfParagraphTests.swift; sourceTree = "<group>"; };
B5AF89311E93CFC80051EFDB /* RenderableAttachmentDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RenderableAttachmentDelegate.swift; sourceTree = "<group>"; };
B5B96DAA1E01B2F300791315 /* UIPasteboard+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIPasteboard+Helpers.swift"; sourceTree = "<group>"; };
B5BC4FED1DA2C17800614582 /* NSAttributedString+Lists.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+Lists.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -507,6 +511,7 @@
B5B96DAA1E01B2F300791315 /* UIPasteboard+Helpers.swift */,
B5C99D3E1E72E2E700335355 /* UIStackView+Helpers.swift */,
F1DE83D41EF20493009269E6 /* UIColor+Parsers.swift */,
B5AB79F71F5F3E0B00DF26F5 /* String+EndOfParagraph.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -774,6 +779,7 @@
B5375F481EC2569500F5D7EC /* StringHTMLTests.swift */,
FF152D8D1E68552A00FF596C /* StringRangeConversionTests.swift */,
FF7DCB4A1E815F9400AB77CB /* UIColorHexParserTests.swift */,
B5AB79F91F5F403C00DF26F5 /* StringEndOfParagraphTests.swift */,
);
name = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -964,6 +970,7 @@
F12F586C1EF20394008AE298 /* HTMLParagraphFormatter.swift in Sources */,
F17BC86B1F4E466100398E2B /* NSAttributedString+HTMLInitializer.swift in Sources */,
F12F58661EF20394008AE298 /* StandardAttributeFormatter.swift in Sources */,
B5AB79F81F5F3E0B00DF26F5 /* String+EndOfParagraph.swift in Sources */,
B5A99D841EBA073D00DED081 /* HTMLStorage.swift in Sources */,
F13CE53E1F4DD05E0043368D /* PipelineProcessor.swift in Sources */,
F17BC8AA1F4E512800398E2B /* AttributedStringParser.swift in Sources */,
Expand Down Expand Up @@ -1070,6 +1077,7 @@
B5F84B631E706B720089A76C /* NSAttributedStringAnalyzerTests.swift in Sources */,
F10BE61C1EA7B1DB002E4625 /* NSAttributedStringReplaceOcurrencesTests.swift in Sources */,
B5375F491EC2569500F5D7EC /* StringHTMLTests.swift in Sources */,
B5AB79FA1F5F403C00DF26F5 /* StringEndOfParagraphTests.swift in Sources */,
B57534521F267D63009D4904 /* ArrayHelperTests.swift in Sources */,
F14665451EA7C230008DE2B8 /* NSMutableAttributedStringReplaceOcurrencesTests.swift in Sources */,
F17BC8B51F4E517100398E2B /* AttributedStringParserTests.swift in Sources */,
Expand Down
28 changes: 28 additions & 0 deletions Aztec/Classes/Extensions/String+EndOfParagraph.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation


// MARK: - Paragraph Analysis Helpers
//
extension String {

/// This methods verifies if the receiver string contains an End of Paragraph before the specified index.
///
/// - Parameters:
/// - index: the index to check
///
/// - Returns: `true` if the receiver contains an end-of-paragraph character before the specified Index.
///
func isEndOfParagraph(before index: String.Index) -> Bool {
assert(index != startIndex)

let previousIndex = self.index(before: index)
guard previousIndex != endIndex else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed privately, this will need to change in an upcoming PR since it's impossible to trigger this guard.

Documenting for history.

return true
}

let endingString = substring(with: previousIndex ..< index)
let paragraphSeparators = [String(.carriageReturn), String(.lineFeed), String(.paragraphSeparator)]

return paragraphSeparators.contains(endingString)
}
}
71 changes: 51 additions & 20 deletions Aztec/Classes/TextKit/LayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,38 @@ private extension LayoutManager {
}

let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
//draw blockquotes
textStorage.enumerateAttribute(NSParagraphStyleAttributeName, in: characterRange, options: []){ (object, range, stop) in

// Draw blockquotes
textStorage.enumerateAttribute(NSParagraphStyleAttributeName, in: characterRange, options: []) { (object, range, stop) in
guard let paragraphStyle = object as? ParagraphStyle, !paragraphStyle.blockquotes.isEmpty else {
return
}
let blockquoteIndent = paragraphStyle.blockquoteIndent

let blockquoteIndent = paragraphStyle.blockquoteIndent
let blockquoteGlyphRange = glyphRange(forCharacterRange: range, actualCharacterRange: nil)

enumerateLineFragments(forGlyphRange: blockquoteGlyphRange) { (rect, usedRect, textContainer, glyphRange, stop) in
let paddingWidth = blockquoteIndent + (blockquoteIndent == 0 ? 0 : (Metrics.listTextIndentation / 2))
var paddingHeight: CGFloat = blockquoteIndent == 0 ? 0 : (Metrics.paragraphSpacing / 2)
// Cheking if we this a middle line inside a blockquote paragraph

// Cheking if we this a middle line inside a blockquote paragraph:
// Avoid applying the "Padding Height", otherwise we might cut off the Blockquote's BG.
//
// Ref. Issue #645
//
let lineRange = self.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
let lineCharacters = textStorage.attributedSubstring(from: lineRange).string
if !lineCharacters.isEndOfLine(before: lineCharacters.endIndex) {

if !lineCharacters.isEndOfParagraph(before: lineCharacters.endIndex) {
paddingHeight = 0
}

let lineRect = rect.offsetBy(dx: origin.x , dy: origin.y)
let finalRect = CGRect(x: lineRect.origin.x + paddingWidth, y: lineRect.origin.y, width: lineRect.size.width - paddingWidth, height: lineRect.size.height - (paddingHeight*2))
let finalRect = CGRect(x: lineRect.origin.x + paddingWidth,
y: lineRect.origin.y,
width: lineRect.size.width - paddingWidth,
height: lineRect.size.height - paddingHeight)

self.drawBlockquote(in: finalRect.integral, with: context)
}
}
Expand Down Expand Up @@ -143,28 +155,47 @@ private extension LayoutManager {

guard textStorage.string.isStartOfNewLine(atUTF16Offset: enclosingRange.location),
let paragraphStyle = textStorage.attribute(NSParagraphStyleAttributeName, at: enclosingRange.location, effectiveRange: nil) as? ParagraphStyle,
let list = paragraphStyle.lists.last else {
return
let list = paragraphStyle.lists.last
else {
return
}

let glyphRange = self.glyphRange(forCharacterRange: enclosingRange, actualCharacterRange: nil)

// Since only the first line in a paragraph can have a bullet, we only need the first line fragment.
//
let lineFragmentRect = self.lineFragmentRect(forGlyphAt: glyphRange.location, effectiveRange: nil)
let lineFragmentRectWithOffset = lineFragmentRect.offsetBy(dx: origin.x, dy: origin.y)

let markerRect = rectForItem(range: glyphRange, origin: origin, paragraphStyle: paragraphStyle)
let markerNumber = textStorage.itemNumber(in: list, at: enclosingRange.location)

self.drawItem(
number: markerNumber,
in: lineFragmentRectWithOffset,
from: list,
using: paragraphStyle,
at: enclosingRange.location)
drawItem(number: markerNumber, in: markerRect, from: list, using: paragraphStyle, at: enclosingRange.location)
}
}

/// Returns the Rect for the MarkerItem at the specified Range + Origin, within a given ParagraphStyle.
///
/// - Parameters:
/// - range: List Item's Range
/// - origin: List Origin
/// - paragraphStyle: Container Style
///
/// - Returns: CGRect in which we should render the MarkerItem.
///
private func rectForItem(range: NSRange, origin: CGPoint, paragraphStyle: ParagraphStyle) -> CGRect {
var paddingY = CGFloat(0)
var effectiveLineRange = NSRange.zero

// Since only the first line in a paragraph can have a bullet, we only need the first line fragment.
let lineFragmentRect = self.lineFragmentRect(forGlyphAt: range.location, effectiveRange: &effectiveLineRange)

// Whenever we're rendering an Item with multiple lines, within a Blockquote, we need to account for the
// paragraph spacing. Otherwise the Marker will show up slightly off.
//
// Ref. #645
//
if effectiveLineRange.length < range.length && paragraphStyle.blockquotes.isEmpty == false {
paddingY = Metrics.paragraphSpacing
}

return lineFragmentRect.offsetBy(dx: origin.x, dy: origin.y + paddingY)
}


/// Draws the specified List Item Number, at a given location.
///
Expand Down
17 changes: 17 additions & 0 deletions AztecTests/Extensions/StringEndOfParagraphTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import XCTest
@testable import Aztec

class StringEndOfParagraphTests: XCTestCase {

func testIsEndOfParagraphReturnsTrueWheneverTestStringEndsWithCarriageReturn() {
let test = "something" + String(.carriageReturn)

XCTAssertTrue(test.isEndOfParagraph(before: test.endIndex))
}

func testIsEndOfParagraphReturnsFalseWheneverTestStringEndsWithLineSeparator() {
let test = "something" + String(.lineSeparator)

XCTAssertFalse(test.isEndOfParagraph(before: test.endIndex))
}
}