Skip to content

Commit

Permalink
Revised logic for matchTextExact for background style (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep authored Aug 30, 2023
1 parent 3c10be0 commit e31a2e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Proton/Sources/Swift/Base/BackgroundStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public enum RoundedCornerStyle {
public enum BackgroundMode {
/// Background matches the height/width of text with font leading padding all around
case matchText
/// Background matches the height of text with minimal padding all around
/// Background matches the height of text based on font with minimal padding all around.
case matchTextExact
/// Background matches entire line irrespective of font height/used character width in the given line
case matchLine
Expand Down
20 changes: 13 additions & 7 deletions Proton/Sources/Swift/Core/LayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class LayoutManager: NSLayoutManager {
return layoutManagerDelegate?.paragraphStyle ?? NSParagraphStyle()
}

var defaultFont: UIFont {
return layoutManagerDelegate?.font ?? UIFont.preferredFont(forTextStyle: .body)
}

private func drawListMarkers(textStorage: NSTextStorage, listRange: NSRange, attributeValue: Any?) {
var lastLayoutRect: CGRect?
var lastLayoutParaStyle: NSParagraphStyle?
Expand Down Expand Up @@ -281,6 +285,7 @@ class LayoutManager: NSLayoutManager {
let usedRect = usedRect.integral
let rangeIntersection = NSIntersectionRange(bgStyleGlyphRange, lineRange)
let paragraphStyle = textStorage.attribute(.paragraphStyle, at: rangeIntersection.location, effectiveRange: nil) as? NSParagraphStyle ?? self.defaultParagraphStyle
let font = textStorage.attribute(.font, at: rangeIntersection.location, effectiveRange: nil) as? UIFont ?? self.defaultFont
let lineHeightMultiple = max(paragraphStyle.lineHeightMultiple, 1)
var rect = self.boundingRect(forGlyphRange: rangeIntersection, in: textContainer).integral
let lineHeightMultipleOffset = (rect.size.height - rect.size.height/lineHeightMultiple)
Expand All @@ -291,13 +296,15 @@ class LayoutManager: NSLayoutManager {
rect.size.width = contentWidth
}

let inset = self.layoutManagerDelegate?.textContainerInset ?? .zero
switch backgroundStyle.heightMode {
case .matchText,
.matchTextExact:
case .matchTextExact:
rect.origin.y = usedRect.origin.y - (font.pointSize - font.ascender)
rect.origin.y += (font.ascender - font.capHeight)
rect.size.height = font.capHeight + abs(font.descender)
case .matchText:
let styledText = textStorage.attributedSubstring(from: bgStyleGlyphRange)
let drawingOptions = backgroundStyle.heightMode == .matchText ? NSStringDrawingOptions.usesFontLeading : []

let textRect = styledText.boundingRect(with: rect.size, options: drawingOptions, context: nil)
let textRect = styledText.boundingRect(with: rect.size, options: .usesFontLeading, context: nil)

rect.origin.y = usedRect.origin.y + (rect.size.height - textRect.height) + lineHeightMultipleOffset - lineSpacing
rect.size.height = textRect.height - lineHeightMultipleOffset
Expand All @@ -306,10 +313,9 @@ class LayoutManager: NSLayoutManager {
// So it is best to restrict the height just to the line fragment.
rect.origin.y = usedRect.origin.y
rect.size.height = usedRect.height

}

let inset = self.layoutManagerDelegate?.textContainerInset ?? .zero

rects.append(rect.offsetBy(dx: inset.left, dy: inset.top))
}
drawBackground(backgroundStyle: backgroundStyle, rects: rects, currentCGContext: currentCGContext)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e31a2e6

Please sign in to comment.