如何使用NSAttributedString制作下标和上标? [英] How to make subscripts and superscripts using NSAttributedString?

查看:82
本文介绍了如何使用NSAttributedString制作下标和上标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为化学式(H2O,Na ^ 2 +等)下标吗?

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

这可能与NSAttributedString有关,还是有其他更简便的方法来制作下标?

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

推荐答案

可以使用NSAttributedString进行此操作.您要查找的属性常量取决于您的平台.对于Mac OS X,它是NSSuperscriptAttributeName,而在iOS上,它是kCTSuperscriptAttributeName.为下标传递一个负值.

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.

唯一需要注意的是,iOS上的UILabel无法绘制NSAttributedString(但是,对于iOS 6则需要用手指交叉).您可能需要使用核心文字"来绘制文字,或者找到可以绘制NSAttributedStringUILabel的第三方替代品.

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.

这篇关于如何使用NSAttributedString制作下标和上标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆