I made a few attempts to implement a similar label but I'm not sure if it's possible using public APIs. Could you give any tips about how to approach implementing this? Thank you!
Hey there.
There are several ways to do this using available APIs on UITextView. For example, if you're aware of the range of text on that first line, you can invoke API such as caretRectForPosition:, or by accessing the textLayoutManager property and using enumerateTextLayoutFragments(from:options:using:) and getting the height of that first, independent line for the date and returning NO for the very first callback.
By getting this result, you can then set the contentOffset on the UITextView so its position doesn't display that first line (the date) in the viewport. When a user scrolls up, they then see that date, just as Notes and Journal does.
Here's a reference to the APIs mentioned above:
- https://developer.apple.com/documentation/uikit/uitextinput/caretrect(for:)
- https://developer.apple.com/documentation/appkit/nstextlayoutmanager/enumeratetextlayoutfragments(from:options:using:)
- https://developer.apple.com/documentation/uikit/uiscrollview/contentoffset
Thanks!