iOS Highlights / notes in the text #17
-
Could you please provide samples of how highlights / notes can be integrated in iOS (similar to how it is done in Android) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 20 replies
-
(Right now, the following is only implemented on the EPUB navigator) 1. Add a "Highlight" selection menu itemYou can add custom "editing actions" for the selection menu when initializing a navigator, if it supports it. For example, with EPUB: init() {
var config = EPUBNavigatorViewController.Configuration()
config.editingActions.append(
EditingAction(title: "Highlight", action: #selector(makeHighlight))
)
if publication.isProtected {
config.editingActions = [.lookup, .translate]
}
let navigator = EPUBNavigatorViewController(publication: publication, initialLocation: location, resourcesServer: app.server, config: config)
}
@objc func makeHighlight(_ sender: Any) {
// ...
} 2. Create a new highlight in your databaseWhen creating a new highlight, you need to store at least two things in your database:
You can get a @objc func makeHighlight(_ sender: Any) {
guard
let navigator = navigator as? SelectableNavigator,
let currentSelection = navigator.currentSelection
else {
return
}
navigator.clearSelection()
// Save the highlight in your database.
// HighlightRepository and the Highlight model are examples types which should be defined in your app.
highlightRepository.add(Highlight(
locator: currentSelection.locator,
color: Color.yellow
))
// Re-render the highlights.
...
} 3. Render the highlights in the publicationTo render the highlights, you can use the Decorator API on any navigator implementing Every time the highlights stored in your database change (or when creating the navigator), you need to re-apply all your decorations: func render(highlights: [Highlight]) {
guard let navigator = navigator as? DecorableNavigator else {
return
}
navigator.apply(
decorations: highlights.map { $0.decoration },
in: "highlights" // Unique identifier for this group of decorations.
)
}
extension Highlight {
/// Converts the highlight into a `Decoration`.
var decoration: Decoration {
Decoration(
id: String(databaseID),
locator: locator,
style: .highlight(tint: color)
)
}
} The Decorator API is similar to iOS's Diffable Data Sources, so re-applying all your highlights every time the DB changes is not a problem because only the highlights that really changed will be re-rendered by the navigator. 4. Displaying the highlight content in the UIIf you have a UI showing the list of highlights, you can extract the highlight content from the let text = highlight.locator.text.highlight You can also use the locator to jump to the page where the highlight is located. navigator.go(to: highlight.locator) |
Beta Was this translation helpful? Give feedback.
-
I am also willing to implement this "Highlight/Note" feature, Also, Do I need to anything extra except the code you have suggested in above answer? Thank you |
Beta Was this translation helpful? Give feedback.
-
Hi @mickael-menu , can you please let me know how I can enable the note feature in iOS? Which latest Readium SDK supports this for swift? Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello @mickael-menu, I'd like to highlight the fragment with using its html id. Is it possible to create a locator with only an id and href and highlight the fragment? Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi @mickael-menu , But it doesn't work for the highlights close to each other. Any idea how can I fix this ? |
Beta Was this translation helpful? Give feedback.
(Right now, the following is only implemented on the EPUB navigator)
1. Add a "Highlight" selection menu item
You can add custom "editing actions" for the selection menu when initializing a navigator, if it supports it. For example, with EPUB: