-
Notifications
You must be signed in to change notification settings - Fork 83
/
LayoutManager.swift
695 lines (561 loc) · 31.8 KB
/
LayoutManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//
// LayoutManager.swift
// Proton
//
// Created by Rajdeep Kwatra on 11/5/20.
// Copyright © 2020 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
protocol LayoutManagerDelegate: AnyObject {
var typingAttributes: [NSAttributedString.Key: Any] { get }
var selectedRange: NSRange { get }
var paragraphStyle: NSMutableParagraphStyle? { get }
var font: UIFont? { get }
var textColor: UIColor? { get }
var textContainerInset: UIEdgeInsets { get }
var listLineFormatting: LineFormatting { get }
var isLineNumbersEnabled: Bool { get }
var lineNumberFormatting: LineNumberFormatting { get }
var lineNumberWrappingMarker: String? { get }
func listMarkerForItem(at index: Int, level: Int, previousLevel: Int, attributeValue: Any?) -> ListLineMarker
func lineNumberString(for index: Int) -> String?
}
class LayoutManager: NSLayoutManager {
private let defaultBulletColor = UIColor.black
private var counters = [Int: Int]()
private var imagecache = [UIImage: NSCache<NSString, UIImage>]()
weak var layoutManagerDelegate: LayoutManagerDelegate?
override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
super.drawGlyphs(forGlyphRange: glyphsToShow, at: origin)
guard let textStorage = self.textStorage else { return }
textStorage.enumerateAttribute(.listItem, in: textStorage.fullRange, options: []) { (value, range, _) in
if value != nil {
drawListMarkers(textStorage: textStorage, listRange: range, attributeValue: value)
}
}
}
var defaultParagraphStyle: NSParagraphStyle {
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?
var lastLayoutFont: UIFont?
var previousLevel = 0
var level = 0
let defaultFont = self.layoutManagerDelegate?.font ?? UIFont.preferredFont(forTextStyle: .body)
let listIndent = layoutManagerDelegate?.listLineFormatting.indentation ?? 25.0
var prevStyle: NSParagraphStyle?
if listRange.location > 0,
textStorage.attribute(.listItem, at: listRange.location - 1, effectiveRange: nil) != nil {
prevStyle = textStorage.attribute(.paragraphStyle, at: listRange.location - 1, effectiveRange: nil) as? NSParagraphStyle
}
if prevStyle == nil {
counters = [:]
}
var levelToSet = 0
textStorage.enumerateAttribute(.paragraphStyle, in: listRange, options: []) { value, range, _ in
levelToSet = 0
if let paraStyle = (value as? NSParagraphStyle)?.mutableParagraphStyle {
let previousLevel = Int(prevStyle?.firstLineHeadIndent ?? 0)/Int(listIndent)
let currentLevel = Int(paraStyle.firstLineHeadIndent)/Int(listIndent)
if currentLevel - previousLevel > 1 {
levelToSet = previousLevel + 1
let indentation = CGFloat(levelToSet) * listIndent
paraStyle.firstLineHeadIndent = indentation
paraStyle.headIndent = indentation
textStorage.addAttribute(.paragraphStyle, value: paraStyle, range: range)
prevStyle = paraStyle
} else {
prevStyle = value as? NSParagraphStyle
}
}
}
let listGlyphRange = glyphRange(forCharacterRange: listRange, actualCharacterRange: nil)
previousLevel = 0
enumerateLineFragments(forGlyphRange: listGlyphRange) { [weak self] (rect, usedRect, textContainer, glyphRange, stop) in
guard let self = self else { return }
let characterRange = self.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
var newLineRange = NSRange.zero
if characterRange.location > 0 {
newLineRange.location = characterRange.location - 1
newLineRange.length = 1
}
// Determines if previous line is completed i.e. terminates with a newline char. Absence of newline character means that the
// line is wrapping and rendering the number/bullet should be skipped.
var isPreviousLineComplete = true
var skipMarker = false
if newLineRange.length > 0 {
let newLineString = textStorage.substring(from: newLineRange)
isPreviousLineComplete = newLineString == "\n"
skipMarker = textStorage.attribute(.skipNextListMarker, at: newLineRange.location, effectiveRange: nil) != nil
}
let font = textStorage.attribute(.font, at: characterRange.location, effectiveRange: nil) as? UIFont ?? defaultFont
let previousParaStyle: NSParagraphStyle?
if characterRange.location == 0 {
previousParaStyle = nil
} else {
previousParaStyle = textStorage.attribute(.paragraphStyle, at: max(characterRange.location - 1, 0), effectiveRange: nil) as? NSParagraphStyle
}
let paraStyle = textStorage.attribute(.paragraphStyle, at: characterRange.location, effectiveRange: nil) as? NSParagraphStyle ?? self.defaultParagraphStyle
previousLevel = Int(previousParaStyle?.firstLineHeadIndent ?? 0)/Int(listIndent)
if isPreviousLineComplete, skipMarker == false {
level = Int(paraStyle.firstLineHeadIndent/listIndent)
var index = (self.counters[level] ?? 0)
self.counters[level] = index + 1
// reset index counter for level when list indentation (level) changes.
if level > previousLevel, level > 1 {
index = 0
self.counters[level] = 1
}
var adjustedRect = rect
// Account for height of line fragment based on styles defined in paragraph, like paragraphSpacing
adjustedRect.size.height = usedRect.height
if level > 0 {
self.drawListItem(level: level, previousLevel: previousLevel, index: index, rect: adjustedRect, paraStyle: paraStyle, font: font, attributeValue: attributeValue)
}
// TODO: should this be moved inside level > 0 check above?
}
lastLayoutParaStyle = paraStyle
lastLayoutRect = rect
lastLayoutFont = font
previousLevel = level
}
var skipMarker = false
if textStorage.length > 0 {
let range = NSRange(location: textStorage.length - 1, length: 1)
let lastChar = textStorage.substring(from: range)
skipMarker = lastChar == "\n" && textStorage.attribute(.skipNextListMarker, at: range.location, effectiveRange: nil) != nil
}
guard skipMarker == false,
let lastRect = lastLayoutRect,
textStorage.length > 1,
textStorage.substring(from: NSRange(location: listRange.endLocation - 1, length: 1)) == "\n",
let paraStyle = lastLayoutParaStyle
else { return }
var index = (counters[level] ?? 0)
let origin = CGPoint(x: lastRect.minX, y: lastRect.maxY)
var para: NSParagraphStyle?
if textStorage.length > listRange.endLocation {
para = textStorage.attribute(.paragraphStyle, at: listRange.endLocation, effectiveRange: nil) as? NSParagraphStyle
let paraLevel = Int((para?.firstLineHeadIndent ?? 0)/listIndent)
// don't draw last rect if there's a following list item (in another indent level)
if para != nil, paraLevel != level {
return
}
}
let newLineRect = CGRect(origin: origin, size: lastRect.size)
if level > previousLevel, level > 1 {
index = 0
counters[level] = 1
}
previousLevel = level
let font = lastLayoutFont ?? defaultFont
drawListItem(level: level, previousLevel: previousLevel, index: index, rect: newLineRect.integral, paraStyle: paraStyle, font: font, attributeValue: attributeValue)
}
private func drawListItem(level: Int, previousLevel: Int, index: Int, rect: CGRect, paraStyle: NSParagraphStyle, font: UIFont, attributeValue: Any?) {
guard level > 0 else { return }
let color = layoutManagerDelegate?.textColor ?? self.defaultBulletColor
color.set()
let marker = layoutManagerDelegate?.listMarkerForItem(at: index, level: level, previousLevel: previousLevel, attributeValue: attributeValue) ?? .string(NSAttributedString(string: "*"))
let listMarkerImage: UIImage
let markerRect: CGRect
// let topInset = layoutManagerDelegate?.textContainerInset.top ?? 0
switch marker {
case let .string(text):
let markerSize = text.boundingRect(with: CGSize(width: paraStyle.firstLineHeadIndent, height: rect.height), options: [], context: nil).size
markerRect = rectForNumberedList(markerSize: markerSize, rect: rect, indent: paraStyle.firstLineHeadIndent, yOffset: paraStyle.paragraphSpacingBefore)
listMarkerImage = self.generateBitmap(string: text, rect: markerRect)
case let .image(image, size):
markerRect = rectForBullet(markerSize: size, rect: rect, indent: paraStyle.firstLineHeadIndent, yOffset: paraStyle.paragraphSpacingBefore)
listMarkerImage = resizedImage(image: image, size: markerRect.size)
}
// let lineSpacing = paraStyle.lineSpacing
let lineHeightMultiple = max(paraStyle.lineHeightMultiple, 1)
let lineHeightMultipleOffset = (rect.size.height - rect.size.height/lineHeightMultiple)
listMarkerImage.draw(at: markerRect.offsetBy(dx: 0, dy: lineHeightMultipleOffset).origin)
}
private func generateBitmap(string: NSAttributedString, rect: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: rect.size)
let image = renderer.image { context in
string.draw(at: .zero)
}
return image
}
private func resizedImage(image: UIImage, size: CGSize) -> UIImage {
let result: UIImage
let sizeCache = imagecache[image, default: .init()]
imagecache.updateValue(sizeCache, forKey: image)
let key = size.debugDescription as NSString
if let cachedImage = sizeCache.object(forKey: key) {
result = cachedImage
} else {
result = image.resizeImage(to: size)
sizeCache.setObject(result, forKey: key)
}
return result
}
private func rectForBullet(markerSize: CGSize, rect: CGRect, indent: CGFloat, yOffset: CGFloat) -> CGRect {
let topInset = layoutManagerDelegate?.textContainerInset.top ?? 0
let leftInset = layoutManagerDelegate?.textContainerInset.left ?? 0
let spacerRect = CGRect(origin: CGPoint(x: rect.minX + leftInset, y: rect.minY + topInset), size: CGSize(width: indent, height: rect.height))
let scaleFactor = markerSize.height / spacerRect.height
var markerSizeToUse = markerSize
// Resize maintaining aspect ratio if bullet height is more than available line height
if scaleFactor > 1 {
markerSizeToUse = CGSize(width: markerSize.width/scaleFactor, height: markerSize.height/scaleFactor)
}
let stringRect = CGRect(origin: CGPoint(x: spacerRect.maxX - markerSizeToUse.width, y: spacerRect.midY - markerSizeToUse.height/2), size: markerSizeToUse)
return stringRect
}
private func rectForNumberedList(markerSize: CGSize, rect: CGRect, indent: CGFloat, yOffset: CGFloat) -> CGRect {
let topInset = layoutManagerDelegate?.textContainerInset.top ?? 0
let leftInset = layoutManagerDelegate?.textContainerInset.left ?? 0
let spacerRect = CGRect(origin: CGPoint(x: rect.minX + leftInset, y: rect.minY + topInset), size: CGSize(width: indent, height: rect.height))
let scaleFactor = markerSize.height / spacerRect.height
var markerSizeToUse = markerSize
// Resize maintaining aspect ratio if bullet height is more than available line height
if scaleFactor > 1 {
markerSizeToUse = CGSize(width: markerSize.width/scaleFactor, height: markerSize.height/scaleFactor)
}
let stringRect = CGRect(origin: CGPoint(x: spacerRect.maxX - markerSizeToUse.width, y: spacerRect.minY + yOffset), size: markerSizeToUse)
return stringRect
}
private func rectForLineNumbers(markerSize: CGSize, rect: CGRect, width: CGFloat) -> CGRect {
let topInset = layoutManagerDelegate?.textContainerInset.top ?? 0
let spacerRect = CGRect(origin: CGPoint(x: 0, y: topInset), size: CGSize(width: width, height: rect.height))
let scaleFactor = markerSize.height / spacerRect.height
var markerSizeToUse = markerSize
// Resize maintaining aspect ratio if bullet height is more than available line height
if scaleFactor > 1 {
markerSizeToUse = CGSize(width: markerSize.width/scaleFactor, height: markerSize.height/scaleFactor)
}
let trailingPadding: CGFloat = 2
let yPos = topInset + rect.minY
let stringRect = CGRect(origin: CGPoint(x: spacerRect.maxX - markerSizeToUse.width - trailingPadding, y: yPos), size: markerSizeToUse)
// debugRect(rect: spacerRect, color: .blue)
// debugRect(rect: stringRect, color: .red)
return stringRect
}
override func drawsOutsideLineFragment(forGlyphAt glyphIndex: Int) -> Bool {
true
}
var hasLineSpacing: Bool {
var lineCount = 0
guard let textStorage else { return false}
enumerateLineFragments(forGlyphRange: textStorage.fullRange, using: { _, _, _, _, stop in
lineCount += 1
if lineCount > 1 {
stop.pointee = true
}
})
return lineCount > 1 || (lineCount > 0 && extraLineFragmentRect.height > 0)
}
override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
super.drawBackground(forGlyphRange: glyphsToShow, at: origin)
guard let textStorage = textStorage,
let currentCGContext = UIGraphicsGetCurrentContext()
else { return }
currentCGContext.saveGState()
let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
textStorage.enumerateAttribute(.backgroundStyle, in: characterRange) { attr, bgStyleRange, _ in
var rects = [CGRect]()
if let backgroundStyle = attr as? BackgroundStyle {
let bgStyleGlyphRange = self.glyphRange(forCharacterRange: bgStyleRange, actualCharacterRange: nil)
enumerateLineFragments(forGlyphRange: bgStyleGlyphRange) { rect1, usedRect, textContainer, lineRange, _ in
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)
let lineHeightMultipleOffset = (rect.size.height - rect.size.height/lineHeightMultiple)
let lineSpacing = paragraphStyle.lineSpacing
if backgroundStyle.widthMode == .matchText {
let content = textStorage.attributedSubstring(from: rangeIntersection)
let contentWidth = content.boundingRect(with: rect.size, options: [.usesDeviceMetrics, .usesFontLeading], context: nil).width
rect.size.width = contentWidth
}
switch backgroundStyle.heightMode {
case .matchTextExact:
let styledText = textStorage.attributedSubstring(from: bgStyleGlyphRange)
var textRect = styledText.boundingRect(with: rect.size, options: [.usesFontLeading, .usesDeviceMetrics], context: nil)
textRect.origin = rect.origin
textRect.size.width = rect.width
textRect.origin.y += abs(font.descender)
let delta = usedRect.height - (font.lineHeight + font.leading)
textRect.origin.y += delta
let hasLineSpacing = (usedRect.height - font.lineHeight) == paragraphStyle.lineSpacing
let isExtraLineHeight = ((usedRect.height - font.lineHeight) - font.leading) > 0.001
if hasLineSpacing || isExtraLineHeight {
textRect.origin.y -= (paragraphStyle.lineSpacing - font.leading)
}
rect = textRect
case .matchText:
let styledText = textStorage.attributedSubstring(from: bgStyleGlyphRange)
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
case .matchLine:
// Glyphs can take space outside of the line fragment, and we cannot draw outside of it.
// So it is best to restrict the height just to the line fragment.
rect.origin.y = usedRect.origin.y
rect.size.height = usedRect.height
}
// if lineRange.endLocation == textStorage.length, font.leading == 0 {
// rect.origin.y += abs(font.descender/2)
// }
rects.append(rect.offsetBy(dx: origin.x, dy: origin.y))
}
drawBackground(backgroundStyle: backgroundStyle, rects: rects, currentCGContext: currentCGContext)
}
}
drawLineNumbers(textStorage: textStorage, currentCGContext: currentCGContext)
currentCGContext.restoreGState()
}
private func drawLineNumbers(textStorage: NSTextStorage, currentCGContext: CGContext) {
var lineNumber = 1
guard layoutManagerDelegate?.isLineNumbersEnabled == true,
let lineNumberFormatting = layoutManagerDelegate?.lineNumberFormatting else { return }
let lineNumberWrappingMarker = layoutManagerDelegate?.lineNumberWrappingMarker
enumerateLineFragments(forGlyphRange: textStorage.fullRange) { [weak self] rect, usedRect, _, range, _ in
guard let self else { return }
let paraRange = self.textStorage?.mutableString.paragraphRange(for: range).firstCharacterRange
let lineNumberToDisplay = layoutManagerDelegate?.lineNumberString(for: lineNumber) ?? "\(lineNumber)"
if range.location == paraRange?.location {
self.drawLineNumber(lineNumber: lineNumberToDisplay, rect: rect.integral, lineNumberFormatting: lineNumberFormatting, currentCGContext: currentCGContext)
lineNumber += 1
} else if let lineNumberWrappingMarker {
self.drawLineNumber(lineNumber: lineNumberWrappingMarker, rect: rect.integral, lineNumberFormatting: lineNumberFormatting, currentCGContext: currentCGContext)
}
}
// Draw line number for additional new line with \n, if exists
drawLineNumber(lineNumber: "\(lineNumber)", rect: extraLineFragmentRect.integral, lineNumberFormatting: lineNumberFormatting, currentCGContext: currentCGContext)
}
func drawLineNumber(lineNumber: String, rect: CGRect, lineNumberFormatting: LineNumberFormatting, currentCGContext: CGContext) {
let gutterWidth = lineNumberFormatting.gutter.width
let attributes = lineNumberAttributes(lineNumberFormatting: lineNumberFormatting)
let text = NSAttributedString(string: "\(lineNumber)", attributes: attributes)
let markerSize = text.boundingRect(with: .zero, options: [], context: nil).integral.size
let markerRect = self.rectForLineNumbers(markerSize: markerSize, rect: rect, width: gutterWidth)
let listMarkerImage = self.generateBitmap(string: text, rect: markerRect)
listMarkerImage.draw(at: markerRect.origin)
}
private func lineNumberAttributes(lineNumberFormatting: LineNumberFormatting) -> [NSAttributedString.Key: Any] {
let font = lineNumberFormatting.font
let textColor = lineNumberFormatting.textColor
let paraStyle = NSMutableParagraphStyle()
paraStyle.alignment = .right
return [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.paragraphStyle: paraStyle
]
}
private func drawBackground(backgroundStyle: BackgroundStyle, rects: [CGRect], currentCGContext: CGContext) {
currentCGContext.saveGState()
let rectCount = rects.count
let rectArray = rects
let color = backgroundStyle.color
for i in 0..<rectCount {
var previousRect = CGRect.zero
var nextRect = CGRect.zero
let currentRect = rectArray[i].insetIfRequired(by: backgroundStyle.insets)
if currentRect.isEmpty {
continue
}
let cornerRadius: CGFloat
switch backgroundStyle.roundedCornerStyle {
case let .absolute(value):
cornerRadius = value
case let .relative(percent):
cornerRadius = currentRect.height * (percent/100.0)
}
if i > 0 {
previousRect = rectArray[i - 1].insetIfRequired(by: backgroundStyle.insets)
}
if i < rectCount - 1 {
nextRect = rectArray[i + 1].insetIfRequired(by: backgroundStyle.insets)
}
let corners: UIRectCorner
if backgroundStyle.hasSquaredOffJoins {
corners = calculateCornersForSquaredOffJoins(previousRect: previousRect, currentRect: currentRect, nextRect: nextRect, cornerRadius: cornerRadius)
} else {
corners = calculateCornersForBackground(previousRect: previousRect, currentRect: currentRect, nextRect: nextRect, cornerRadius: cornerRadius)
}
let rectanglePath = UIBezierPath(roundedRect: currentRect, byRoundingCorners: corners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
color.set()
currentCGContext.setAllowsAntialiasing(true)
currentCGContext.setShouldAntialias(true)
if let shadowStyle = backgroundStyle.shadow {
currentCGContext.setShadow(offset: shadowStyle.offset, blur: shadowStyle.blur, color: shadowStyle.color.cgColor)
}
currentCGContext.setFillColor(color.cgColor)
currentCGContext.addPath(rectanglePath.cgPath)
currentCGContext.drawPath(using: .fill)
let lineWidth = backgroundStyle.border?.lineWidth ?? 0
let overlappingLine = UIBezierPath()
// TODO: Revisit shadow drawing logic to simplify a bit
let leftVerticalJoiningLine = UIBezierPath()
let rightVerticalJoiningLine = UIBezierPath()
// Shadow for vertical lines need to be drawn separately to get the perfect alignment with shadow on rectangles.
let leftVerticalJoiningLineShadow = UIBezierPath()
let rightVerticalJoiningLineShadow = UIBezierPath()
var lineLength: CGFloat = 0
if backgroundStyle.heightMode != .matchTextExact,
!previousRect.isEmpty, (currentRect.maxX - previousRect.minX) > cornerRadius {
let yDiff = currentRect.minY - previousRect.maxY
var overLapMinX = max(previousRect.minX, currentRect.minX) + lineWidth/2
var overlapMaxX = min(previousRect.maxX, currentRect.maxX) - lineWidth/2
lineLength = overlapMaxX - overLapMinX
// Adjust overlap line length if the rounding on current and previous overlaps
// accounting for relative rounding as it rounds at both top and bottom vs. fixed which rounds
// only at top when in an overlap
if (currentRect.maxX - previousRect.minX <= cornerRadius)
|| (previousRect.minX - currentRect.maxX <= cornerRadius) && backgroundStyle.roundedCornerStyle.isRelative {
overLapMinX += cornerRadius
overlapMaxX -= cornerRadius
}
overlappingLine.move(to: CGPoint(x: overLapMinX , y: previousRect.maxY + yDiff/2))
overlappingLine.addLine(to: CGPoint(x: overlapMaxX, y: previousRect.maxY + yDiff/2))
let leftX = max(previousRect.minX, currentRect.minX)
let rightX = min(previousRect.maxX, currentRect.maxX)
leftVerticalJoiningLine.move(to: CGPoint(x: leftX, y: previousRect.maxY))
leftVerticalJoiningLine.addLine(to: CGPoint(x: leftX, y: currentRect.minY))
rightVerticalJoiningLine.move(to: CGPoint(x: rightX, y: previousRect.maxY))
rightVerticalJoiningLine.addLine(to: CGPoint(x: rightX, y: currentRect.minY))
let leftShadowX = max(previousRect.minX, currentRect.minX) + lineWidth
let rightShadowX = min(previousRect.maxX, currentRect.maxX) - lineWidth
leftVerticalJoiningLineShadow.move(to: CGPoint(x: leftShadowX, y: previousRect.maxY))
leftVerticalJoiningLineShadow.addLine(to: CGPoint(x: leftShadowX, y: currentRect.minY))
rightVerticalJoiningLineShadow.move(to: CGPoint(x: rightShadowX, y: previousRect.maxY))
rightVerticalJoiningLineShadow.addLine(to: CGPoint(x: rightShadowX, y: currentRect.minY))
}
if let borderColor = backgroundStyle.border?.color {
currentCGContext.setLineWidth(lineWidth * 2)
currentCGContext.setStrokeColor(borderColor.cgColor)
// always draw vertical joining lines
currentCGContext.addPath(leftVerticalJoiningLineShadow.cgPath)
currentCGContext.addPath(rightVerticalJoiningLineShadow.cgPath)
currentCGContext.drawPath(using: .stroke)
}
currentCGContext.setShadow(offset: .zero, blur:0, color: UIColor.clear.cgColor)
if !currentRect.isEmpty,
let borderColor = backgroundStyle.border?.color {
currentCGContext.setLineWidth(lineWidth)
currentCGContext.setStrokeColor(borderColor.cgColor)
currentCGContext.addPath(rectanglePath.cgPath)
// always draw vertical joining lines
currentCGContext.addPath(leftVerticalJoiningLine.cgPath)
currentCGContext.addPath(rightVerticalJoiningLine.cgPath)
currentCGContext.drawPath(using: .stroke)
}
// draw over the overlapping bounds of previous and next rect to hide shadow/borders
// if the border color is defined and different from background
// Also, account for rounding so that the overlap line does not eat into rounding lines
if let borderColor = backgroundStyle.border?.color,
lineLength > (cornerRadius * 2),
color != borderColor {
currentCGContext.setStrokeColor(color.cgColor)
currentCGContext.addPath(overlappingLine.cgPath)
}
// account for the spread of shadow
let blur = (backgroundStyle.shadow?.blur ?? 1) * 2
let offsetHeight = abs(backgroundStyle.shadow?.offset.height ?? 1)
currentCGContext.setLineWidth(lineWidth + (currentRect.minY - previousRect.maxY) + blur + offsetHeight + 1)
currentCGContext.drawPath(using: .stroke)
}
currentCGContext.restoreGState()
}
private func calculateCornersForSquaredOffJoins(previousRect: CGRect, currentRect: CGRect, nextRect: CGRect, cornerRadius: CGFloat) -> UIRectCorner {
var corners = UIRectCorner()
let isFirst = previousRect.isEmpty && !currentRect.isEmpty
let isLast = nextRect.isEmpty && !currentRect.isEmpty
if isFirst {
corners.formUnion(.topLeft)
corners.formUnion(.bottomLeft)
}
if isLast {
corners.formUnion(.topRight)
corners.formUnion(.bottomRight)
}
return corners
}
private func calculateCornersForBackground(previousRect: CGRect, currentRect: CGRect, nextRect: CGRect, cornerRadius: CGFloat) -> UIRectCorner {
var corners = UIRectCorner()
if previousRect.minX > currentRect.minX {
corners.formUnion(.topLeft)
}
if previousRect.maxX < currentRect.maxX {
corners.formUnion(.topRight)
}
if currentRect.maxX > nextRect.maxX {
corners.formUnion(.bottomRight)
}
if currentRect.minX < nextRect.minX {
corners.formUnion(.bottomLeft)
}
if nextRect.isEmpty || nextRect.maxX <= currentRect.minX + cornerRadius {
corners.formUnion(.bottomLeft)
corners.formUnion(.bottomRight)
}
if previousRect.isEmpty || (currentRect.maxX <= previousRect.minX + cornerRadius) {
corners.formUnion(.topLeft)
corners.formUnion(.topRight)
}
return corners
}
// Helper function to debug rectangles by drawing in context
private func debugRect(rect: CGRect, color: UIColor) {
let path = UIBezierPath(rect: rect).cgPath
debugPath(path: path, color: color)
}
// Helper function to debug Bezier Path by drawing in context
private func debugPath(path: CGPath, color: UIColor) {
let currentCGContext = UIGraphicsGetCurrentContext()
currentCGContext?.saveGState()
currentCGContext?.setStrokeColor(color.cgColor)
currentCGContext?.addPath(path)
currentCGContext?.drawPath(using: .stroke)
currentCGContext?.restoreGState()
}
}
extension CGRect {
func insetIfRequired(by insets: UIEdgeInsets) -> CGRect {
return isEmpty ? self : inset(by: insets)
}
}
extension UIImage {
func resizeImage(to size: CGSize) -> UIImage {
let renderer = UIGraphicsImageRenderer(
size: size
)
let scaledImage = renderer.image { _ in
self.draw(in: CGRect(
origin: .zero,
size: size
))
}
return scaledImage
}
}
extension CGFloat {
func isBetween(_ first: CGFloat, _ second: CGFloat) -> Bool {
return self > first && self < second
}
}