How to make subscripts and superscripts using NSAttributedString?

前端 未结 5 1305
情书的邮戳
情书的邮戳 2020-11-27 15:40

I need to make subscripts for chemistry formulas (H2O, Na^2+, etc)?

Is this possible to do with NSAttributedString, or is there an alternative/easier way to make sub

5条回答
  •  半阙折子戏
    2020-11-27 16:14

    This is possible to do with NSAttributedString. The attribute constant you're looking for depends on your platform. For Mac OS X it is NSSuperscriptAttributeName and on iOS it is kCTSuperscriptAttributeName. Pass in a negative value for subscript.

    The only caveat is that UILabel on iOS can't draw NSAttributedStrings (yet, fingers crossed for iOS 6). You would need to draw the text using Core Text or find some third party replacement for UILabel that can draw an NSAttributedString.

提交回复
热议问题