【问题标题】:superscript characters in string IOS字符串IOS中的上标字符
【发布时间】:2012-05-17 17:48:28
【问题描述】:

我在 xcode 中的应用程序有一个包含一些产品数据的表格视图。我的一款产品的名称中有一个上标 e。如何在字符串中使用上标字符,例如: texte

我可以让它处理数字:text\u2070 -> text0 或 text\u2071 -> text1。但是如何处理其他角色呢?

谢谢!

【问题讨论】:

  • 也许这会有所帮助:stackoverflow.com/questions/5080175/…
  • 使用@JasonWang 给你的链接。我们遇到了完全相同的问题,我们确实像那里所说的那样,使用 webview 和 html。没有其他解决方案
  • 它有一些很好的信息,但我不能在 tableview 中使用 webview 之类的东西。是用图片做到这一点的唯一方法吗?

标签: ios xcode character-encoding tableview superscript


【解决方案1】:

对于下标,kCTSuperscriptAttributeName 的值为@-1。

根据文档

@discussion 值必须是 CFNumberRef。默认值为 int 值 0。如果 受指定字体支持,值为 1 启用上标 值 -1 启用下标。

extern const CFStringRef kCTSuperscriptAttributeName CT_AVAILABLE(10_5, 3_2);

Example- [lblHeader setText:@“Headers [Alpha1 – text”];

        NSMutableAttributedString *headerSubscript = [[NSMutableAttributedString alloc]initWithAttributedString: lblHeader.attributedText];

        [headerSubscript addAttribute:(NSString *)kCTSuperscriptAttributeName value:@-1 range:NSMakeRange(14,1)];      

        [lblHeader setAttributedText:headerSubscript];

【讨论】:

    【解决方案2】:

    添加 CoreText 框架,导入 CoreText.h,并使用 UIlabel.attributedText - 它完全支持 NSAttributedString。已经在 SO 上反复询问和回答。

    【讨论】:

      【解决方案3】:

      tl;dr: NSString 不支持超级/子脚本的概念。这或多或少是一个 UI 格式问题。

      一种可能的解决方案是在代码中动态添加 UILabel 而不是界面构建器。您可以添加第二个 UILabel,其字体大小较小。

      【讨论】:

      • 感谢您的信息!字体较小的 UILabel 看起来是最好的方法。
      猜你喜欢
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多