Various improvements

This commit is contained in:
Ali
2023-10-23 20:36:17 +04:00
parent 4790c3c5b9
commit f957979db9
3 changed files with 88 additions and 11 deletions

View File

@@ -70,10 +70,12 @@ public struct TextRangeRectEdge: Equatable {
public final class TextNodeBlockQuoteData: NSObject {
public let title: NSAttributedString?
public let color: UIColor
public let secondaryColor: UIColor?
public init(title: NSAttributedString?, color: UIColor) {
public init(title: NSAttributedString?, color: UIColor, secondaryColor: UIColor?) {
self.title = title
self.color = color
self.secondaryColor = secondaryColor
super.init()
}
@@ -93,6 +95,13 @@ public final class TextNodeBlockQuoteData: NSObject {
if !self.color.isEqual(other.color) {
return false
}
if let lhsSecondaryColor = self.secondaryColor, let rhsSecondaryColor = other.secondaryColor {
if !lhsSecondaryColor.isEqual(rhsSecondaryColor) {
return false
}
} else if (self.secondaryColor == nil) != (other.secondaryColor == nil) {
return false
}
return true
}
@@ -131,10 +140,12 @@ private final class TextNodeLine {
private final class TextNodeBlockQuote {
let frame: CGRect
let tintColor: UIColor
let secondaryTintColor: UIColor?
init(frame: CGRect, tintColor: UIColor) {
init(frame: CGRect, tintColor: UIColor, secondaryTintColor: UIColor?) {
self.frame = frame
self.tintColor = tintColor
self.secondaryTintColor = secondaryTintColor
}
}
@@ -1200,6 +1211,7 @@ open class TextNode: ASDisplayNode {
let firstCharacterOffset: Int
let isBlockQuote: Bool
let tintColor: UIColor?
let secondaryTintColor: UIColor?
}
var stringSegments: [StringSegment] = []
@@ -1222,7 +1234,8 @@ open class TextNode: ASDisplayNode {
)),
firstCharacterOffset: segmentCharacterOffset,
isBlockQuote: false,
tintColor: nil
tintColor: nil,
secondaryTintColor: nil
))
}
@@ -1233,7 +1246,8 @@ open class TextNode: ASDisplayNode {
substring: attributedString.attributedSubstring(from: effectiveRange),
firstCharacterOffset: effectiveRange.location,
isBlockQuote: true,
tintColor: value.color
tintColor: value.color,
secondaryTintColor: value.secondaryColor
))
}
segmentCharacterOffset = effectiveRange.location + effectiveRange.length
@@ -1246,7 +1260,8 @@ open class TextNode: ASDisplayNode {
substring: attributedString.attributedSubstring(from: effectiveRange),
firstCharacterOffset: effectiveRange.location,
isBlockQuote: false,
tintColor: nil
tintColor: nil,
secondaryTintColor: nil
))
segmentCharacterOffset = effectiveRange.location + effectiveRange.length
}
@@ -1261,7 +1276,8 @@ open class TextNode: ASDisplayNode {
)),
firstCharacterOffset: segmentCharacterOffset,
isBlockQuote: false,
tintColor: nil
tintColor: nil,
secondaryTintColor: nil
))
}
@@ -1273,6 +1289,7 @@ open class TextNode: ASDisplayNode {
var titleLine: TextNodeLine?
var lines: [TextNodeLine] = []
var tintColor: UIColor?
var secondaryTintColor: UIColor?
var isBlockQuote: Bool = false
var additionalWidth: CGFloat = 0.0
}
@@ -1283,6 +1300,7 @@ open class TextNode: ASDisplayNode {
var calculatedSegment = CalculatedSegment()
calculatedSegment.isBlockQuote = segment.isBlockQuote
calculatedSegment.tintColor = segment.tintColor
calculatedSegment.secondaryTintColor = segment.secondaryTintColor
let rawSubstring = segment.substring.string as NSString
let substringLength = rawSubstring.length
@@ -1484,7 +1502,7 @@ open class TextNode: ASDisplayNode {
}
if segment.isBlockQuote, let tintColor = segment.tintColor {
blockQuotes.append(TextNodeBlockQuote(frame: CGRect(origin: CGPoint(x: 0.0, y: blockMinY - 2.0), size: CGSize(width: blockWidth, height: blockMaxY - (blockMinY - 2.0) + 4.0)), tintColor: tintColor))
blockQuotes.append(TextNodeBlockQuote(frame: CGRect(origin: CGPoint(x: 0.0, y: blockMinY - 2.0), size: CGSize(width: blockWidth, height: blockMaxY - (blockMinY - 2.0) + 4.0)), tintColor: tintColor, secondaryTintColor: segment.secondaryTintColor))
}
}
@@ -2178,7 +2196,46 @@ open class TextNode: ASDisplayNode {
context.addArc(tangent1End: CGPoint(x: lineFrame.minX, y: lineFrame.maxY), tangent2End: CGPoint(x: lineFrame.minX, y: lineFrame.maxY - radius), radius: radius)
context.closePath()
context.clip()
context.fill(lineFrame)
if let secondaryTintColor = blockQuote.secondaryTintColor {
let isMonochrome = secondaryTintColor.alpha == 0.0
do {
context.saveGState()
if isMonochrome {
context.setFillColor(blockQuote.tintColor.withMultipliedAlpha(0.2).cgColor)
context.fill(lineFrame)
context.setFillColor(blockQuote.tintColor.cgColor)
} else {
context.setFillColor(blockQuote.tintColor.cgColor)
context.fill(lineFrame)
context.setFillColor(secondaryTintColor.cgColor)
}
let dashOffset: CGFloat = isMonochrome ? -4.0 : 5.0
context.translateBy(x: blockFrame.minX, y: blockFrame.minY + dashOffset)
var offset = 0.0
while offset < blockFrame.height {
context.move(to: CGPoint(x: 0.0, y: 3.0))
context.addLine(to: CGPoint(x: lineWidth, y: 0.0))
context.addLine(to: CGPoint(x: lineWidth, y: 9.0))
context.addLine(to: CGPoint(x: 0.0, y: 9.0 + 3.0))
context.closePath()
context.fillPath()
context.translateBy(x: 0.0, y: 18.0)
offset += 18.0
}
context.restoreGState()
}
} else {
context.setFillColor(blockQuote.tintColor.cgColor)
context.fill(lineFrame)
}
context.resetClip()
}