How to display NSMutableAttributedString within ScrollView on iPhone
-
28-09-2019 - |
문제
I am developing an iPhone app and want to be able to use an NSMutableAttributedString inside a ScrollView. Here are the steps I want to follow:
- Grab a string from CoreData.
- Copy the string into an NSMutableAttributedString.
- Parse the string and modify the attributes of portions of the text.
- Display the NSMutableAttributedString inside a ScrollView.
How can I do this?
If I am reading the docs correctly, only CoreText naitively supports display of attributed strings on the iPhone. Is that correct? If so, how can I create a CoreText object when I don't know how the Y value for the CGRect?
해결책
Upon doing more research and experimentation, I found that there are methods available to calculate the size of the Rects used to draw CoreText. I looked at the code from http://github.com/jonasschnelli/I7CoreTextExample as a starting point for my project. Although there are a couple of bugs in the code, it does provide a good base understanding of how CoreText can be used.
By the way, although the code is set up to run on the iPad, the same code works on an iPhone. I hope this helps someone.