使用 unicode 表示分数的 NSString
我试图在我的 iPhone 应用程序中“很好地”显示分数。以前我一直在使用一个繁琐的 switch 语句,导致粗俗分数的硬编码 unicode 字符,但我已经了解了 unicode 分数斜杠字符,如果我理解正确的话,它应该意味着我可以创建一个字符串,如下所示
[NSString stringWithFormat:@"%i\u2044%i",numerator,denominator];
: “渲染器”将自动使用更小的上标分子和下标分母来打印它。然而,上面的代码只是给了我标准的 1/2 外观。我正在使用drawAtPoint 将字符串放在屏幕上。我已经尝试过 decomposedStringUsingCanonicalMapping 和 precomposedStringUsingCanonicalMapping 但说实话,文档让我迷失了。
这应该有效还是 NSString 绘图无法解决这个问题?
I am trying to "nicely" display fractions in my iPhone application. Previously I have been using a tedious switch statement leading to hardcoded unicode characters for the vulgar fractions, but I have learnt about the unicode fraction slash character which, if I am understanding it correctly, should mean that I can create a string as follows:
[NSString stringWithFormat:@"%i\u2044%i",numerator,denominator];
And the "renderer" will automatically print it with a smaller, superscriped numerator and subscripted denominator. However, the above code just gives me the standard 1/2 appearance. I am using drawAtPoint to put the string on the screen. I have experimented with decomposedStringUsingCanonicalMapping and precomposedStringUsingCanonicalMapping but to be honest the documentation lost me.
Should this be working or does NSString drawing not cope with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我碰巧只想将食谱的简单分数转换为 Unicode 粗俗分数。
您可以这样做:
I happened to only want simple fractions for recipes to be converted to Unicode vulgar fractions.
Here is how you can do it:
我不知道 unicode 字符有什么方法可以具有您所描述的属性。据我所知,U+2044 与常规斜线的唯一区别是它的角度更大一些,并且两侧几乎没有空间,因此使其与周围的数字更接近。
这是一个关于在 HTML 中使用分数斜杠的页面,如您所见表明如果您尝试自己使用它,您只会得到类似“1⁄10”的结果。它通过在周围数字上使用 HTML 中的
和
标签来补偿这一点,以获得适当的显示。
为了让你在 NSString 中使用它,你必须找到一些方法来自己对周围的数字应用上标和下标。
I'm not aware of any way for a unicode character to have the properties you describe. AFAIK the only thing that distinguishes U+2044 from a regular slash is it's a bit more angled and has little-to-no space on either side, therefore making it nestle up a lot closer to the surrounding numbers.
Here's a page on using the Fraction Slash in HTML, and as you can see it demonstrates that you simply get something like "1⁄10" if you try and use it on your own. It compensates for this by using the
<sup>
and<sub>
tags in HTML on the surrounding numbers to get an appropriate display.In order for you to get this to work in NSString you're going to have to figure out some way to apply superscripting and subscripting to the surrounding numbers yourself.
有一些包含的 Unicode 字符确实给出了实际的分数外观,但我认为它们仅限于 1/2、1/3 和 1/4。
如果您想要任意分数,在我看来您需要一个自定义视图来绘制适当的外观;通过使用定位子视图或
drawRect:
。There are some included Unicode chars that do give actual fraction appearances, but they're limited to a 1/2, a 1/3 and a 1/4 I think.
If you want this for arbitrary fractions, seems to me like you need a custom view that draws the appropriate look; either through using positioned subviews or
drawRect:
.我知道这是很久以前的事了,但如果有帮助的话,所有十进制数字都有上标 Unicode 字符,您可以使用它来显示大多数字体中的任意分数;在这里查看类似问题的答案 - https://stackoverflow.com/a/30860163/4522315
编辑:
根据评论,此解决方案取决于您使用的字体。 Amsi Pro(左)和其他商业字体往往包含上标所需的所有符号,但系统字体(右)则不然。
I know this was a long time ago, but if it helps, there are superscript Unicode characters for all decimal numbers you can use to display arbitrary fractions in most fonts; see answers on a similar question here - https://stackoverflow.com/a/30860163/4522315
Edit:
As per comments, this solution depends on the font you're using. Amsi Pro (left) and other commercial fonts tend to include all the required symbols for superscripts, but the system font (right) does not.