Skip to content

Commit a8222db

Browse files
committed
Change blockquote rendering
Text is italic, drawn with a green border. (Would be nice to use the tint color but that's not known at VC creation time)
1 parent 0288d02 commit a8222db

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- `CodeSpan` support
88

9+
### Changed
10+
11+
- Formatting for block quotes
12+
913
## [0.11.0] - 2019-09-16
1014

1115
### Added

CommonplaceBookApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
D35C72C32189E0720082396D /* TextCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D35C72C22189E0720082396D /* TextCollectionViewCell.swift */; };
6666
D35E90C7230C5AD300A46B3F /* UserDefault.swift in Sources */ = {isa = PBXBuildFile; fileRef = D35E90C6230C5AD300A46B3F /* UserDefault.swift */; };
6767
D35E90C9230C755D00A46B3F /* UIApplication+Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D35E90C8230C755D00A46B3F /* UIApplication+Environment.swift */; };
68+
D36214662334F607008C8FF4 /* LayoutManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36214652334F607008C8FF4 /* LayoutManager.swift */; };
6869
D36BC17C21B572ED00935691 /* TestMetadataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D36BC17B21B572ED00935691 /* TestMetadataProvider.swift */; };
6970
D374AB56229380E500F54561 /* TemporaryFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = D374AB55229380E500F54561 /* TemporaryFile.swift */; };
7071
D374AB6B229655BA00F54561 /* TextSnippetArchive.swift in Sources */ = {isa = PBXBuildFile; fileRef = D374AB6A229655BA00F54561 /* TextSnippetArchive.swift */; };
@@ -198,6 +199,7 @@
198199
D35C72C22189E0720082396D /* TextCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextCollectionViewCell.swift; sourceTree = "<group>"; };
199200
D35E90C6230C5AD300A46B3F /* UserDefault.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefault.swift; sourceTree = "<group>"; };
200201
D35E90C8230C755D00A46B3F /* UIApplication+Environment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Environment.swift"; sourceTree = "<group>"; };
202+
D36214652334F607008C8FF4 /* LayoutManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutManager.swift; sourceTree = "<group>"; };
201203
D36BC17B21B572ED00935691 /* TestMetadataProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestMetadataProvider.swift; sourceTree = "<group>"; };
202204
D374AB55229380E500F54561 /* TemporaryFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemporaryFile.swift; sourceTree = "<group>"; };
203205
D374AB6A229655BA00F54561 /* TextSnippetArchive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextSnippetArchive.swift; sourceTree = "<group>"; };
@@ -409,6 +411,7 @@
409411
D3591503231C3AC800883AC4 /* View+Locale.swift */,
410412
D3D50A2C231AC8EB007440F1 /* VocabularyChallengeTemplate.swift */,
411413
D30D261723193F9A0031AA20 /* VocabularyViewController.swift */,
414+
D36214652334F607008C8FF4 /* LayoutManager.swift */,
412415
);
413416
path = CommonplaceBookApp;
414417
sourceTree = "<group>";
@@ -674,6 +677,7 @@
674677
D35C72C32189E0720082396D /* TextCollectionViewCell.swift in Sources */,
675678
D37E6D34228D7AC6006CD0FA /* Observable.swift in Sources */,
676679
D348A97A21C6C586005CA937 /* FileNameGenerator.swift in Sources */,
680+
D36214662334F607008C8FF4 /* LayoutManager.swift in Sources */,
677681
D31AFAC3229D61EE00ED2B76 /* String+Typography.swift in Sources */,
678682
D3591500231C205500883AC4 /* EditVocabularyView.swift in Sources */,
679683
D3E4476023254FAF00F21F91 /* EncodedImage.swift in Sources */,

CommonplaceBookApp.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright © 2017-present Brian's Brain. All rights reserved.
2+
3+
import Foundation
4+
import Logging
5+
import MiniMarkdown
6+
import UIKit
7+
8+
private let logger = Logger(label: "org.brians-brian.CommonplaceBookApp.LayoutManager")
9+
10+
/// Custom layout manager that knows how to draw vertical bars next to block quotes.
11+
/// Implementation inspired by the Wordpress Aztec HTML editing component:
12+
/// https://github.com/wordpress-mobile/AztecEditor-iOS/blob/develop/Aztec/Classes/TextKit/LayoutManager.swift
13+
final class LayoutManager: NSLayoutManager {
14+
override func drawBackground(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
15+
super.drawBackground(forGlyphRange: glyphsToShow, at: origin)
16+
drawBlockquotes(forGlyphRange: glyphsToShow, at: origin)
17+
}
18+
}
19+
20+
// MARK: - Private
21+
22+
private extension LayoutManager {
23+
/// Text with a `.blockquoteBorderColor` attribute gets rendered as a block quote:
24+
/// - The background is `quaternarySystemFill`
25+
/// - A 4 point border on the left edge is filled with `blockquoteBorderColor`
26+
func drawBlockquotes(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
27+
guard let textStorage = textStorage else {
28+
return
29+
}
30+
31+
guard let context = UIGraphicsGetCurrentContext() else {
32+
preconditionFailure("When drawBackgroundForGlyphRange is called, the graphics context is supposed to be set by UIKit")
33+
}
34+
35+
let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
36+
textStorage.enumerateAttribute(.blockquoteBorderColor, in: characterRange, options: []) { object, range, _ in
37+
guard let color = object as? UIColor else {
38+
return
39+
}
40+
logger.debug("Drawing a vertical bar")
41+
let verticalBarGlyphRange = glyphRange(forCharacterRange: range, actualCharacterRange: nil)
42+
enumerateLineFragments(forGlyphRange: verticalBarGlyphRange) { rect, _, _, _, _ in
43+
var verticalBarRect = rect.offsetBy(dx: origin.x, dy: origin.y)
44+
UIColor.quaternarySystemFill.setFill()
45+
context.fill(verticalBarRect)
46+
verticalBarRect.size.width = 4
47+
color.setFill()
48+
context.fill(verticalBarRect)
49+
}
50+
}
51+
}
52+
}

CommonplaceBookApp/TextEditViewController.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ final class TextEditViewController: UIViewController {
114114
}
115115
}
116116
formatters[.bold] = { $1.bold = true }
117-
formatters[.emphasis] = { $1.italic = true }
117+
formatters[.emphasis] = { $1.italic.toggle() }
118118
formatters[.table] = { $1.familyName = "Menlo" }
119119
formatters[.codeSpan] = { $1.familyName = "Menlo" }
120120
formatters[.cloze] = { $1.backgroundColor = UIColor.systemYellow.withAlphaComponent(0.3) }
@@ -123,7 +123,8 @@ final class TextEditViewController: UIViewController {
123123
}
124124
formatters[.hashtag] = { $1.backgroundColor = UIColor.secondarySystemBackground }
125125
formatters[.blockQuote] = {
126-
$1.backgroundColor = UIColor.secondarySystemBackground
126+
$1.italic = true
127+
$1.blockquoteBorderColor = UIColor.systemGreen
127128
$1.listLevel += 1
128129
}
129130
return formatters
@@ -172,7 +173,7 @@ final class TextEditViewController: UIViewController {
172173
}
173174

174175
private lazy var textView: UITextView = {
175-
let layoutManager = NSLayoutManager()
176+
let layoutManager = LayoutManager()
176177
textStorage.addLayoutManager(layoutManager)
177178
let textContainer = NSTextContainer()
178179
layoutManager.addTextContainer(textContainer)

0 commit comments

Comments
 (0)