简体   繁体   English

如何在iPhone的ScrollView中显示NSMutableAttributedString

[英]How to display NSMutableAttributedString within ScrollView on iPhone

I am developing an iPhone app and want to be able to use an NSMutableAttributedString inside a ScrollView. 我正在开发iPhone应用程序,并希望能够在ScrollView中使用NSMutableAttributedString。 Here are the steps I want to follow: 这是我要遵循的步骤:

  1. Grab a string from CoreData. 从CoreData抓取一个字符串。
  2. Copy the string into an NSMutableAttributedString. 将字符串复制到NSMutableAttributedString中。
  3. Parse the string and modify the attributes of portions of the text. 解析字符串并修改文本各部分的属性。
  4. Display the NSMutableAttributedString inside a ScrollView. 在ScrollView中显示NSMutableAttributedString。

How can I do this? 我怎样才能做到这一点?

If I am reading the docs correctly, only CoreText naitively supports display of attributed strings on the iPhone. 如果我正确阅读了文档,则只有CoreText天真地支持在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? 如果是这样,当我不知道CGRect的Y值如何时如何创建CoreText对象?

Upon doing more research and experimentation, I found that there are methods available to calculate the size of the Rects used to draw CoreText. 经过更多的研究和实验,我发现有一些方法可以计算用于绘制CoreText的Rect的大小。 I looked at the code from http://github.com/jonasschnelli/I7CoreTextExample as a starting point for my project. 我查看了来自http://github.com/jonasschnelli/I7CoreTextExample的代码,将其作为我项目的起点。 Although there are a couple of bugs in the code, it does provide a good base understanding of how CoreText can be used. 尽管代码中有两个错误,但它确实为如何使用CoreText提供了良好的基础理解。

By the way, although the code is set up to run on the iPad, the same code works on an iPhone. 顺便说一句,尽管该代码被设置为可以在iPad上运行,但是相同的代码可以在iPhone上运行。 I hope this helps someone. 我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM