26

我需要为化学公式(H2O、Na^2+ 等)添加下标吗?

这可能与 NSAttributedString 有关,还是有另一种/更简单的方法来制作下标?

4

5 回答 5

33

这是我在 iOS 6 中所做的。首先添加 CoreText 和 QuartzCore 框架。然后导入:

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CTStringAttributes.h>
#import <CoreText/CoreText.h>

我做了一个小函数,输入一个普通的 NSString 并导出一个带有上标最后一个字符的 NSMutableAttributedString。这可以修改为允许设置上标或下标,将 kCTSuperscriptAttributeName 值更改为 -1。您还可以添加一个变量来指定将上标放在字符串中的位置。现在它只是假设字符串的结尾。

- (NSMutableAttributedString *)plainStringToAttributedUnits:(NSString *)string;
{
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
    UIFont *font = [UIFont systemFontOfSize:10.0f];
    UIFont *smallFont = [UIFont systemFontOfSize:9.0f];

    [attString beginEditing];
    [attString addAttribute:NSFontAttributeName value:(font) range:NSMakeRange(0, string.length - 2)];
    [attString addAttribute:NSFontAttributeName value:(smallFont) range:NSMakeRange(string.length - 1, 1)];
    [attString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:NSMakeRange(string.length - 1, 1)];
    [attString addAttribute:(NSString*)kCTForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length - 1)];
    [attString endEditing];
    return attString;
}

现在,当我想使用它时,我可以执行以下操作将其放入 UITextField:

    NSString *qlwUnitsPlainText = @"m3";
    self.quantityLoadWeightUnits_textField.attributedText = [self plainStringToAttributedUnits:qlwUnitsPlainText];

我希望这对其他人有帮助,那里没有很多例子!

于 2013-02-08T16:48:36.860 回答
28

这可能与NSAttributedString. 您要查找的属性常量取决于您的平台。对于 Mac OS X 是NSSuperscriptAttributeName,在 iOS 上是kCTSuperscriptAttributeName. 为下标传入一个负值。

唯一需要注意的是,UILabel在 iOS 上无法绘制NSAttributedStrings(然而,iOS 6 的手指交叉)。您需要使用 Core Text 绘制文本或找到一些第三方替代品UILabel来绘制NSAttributedString.

于 2011-12-18T23:46:27.350 回答
9

在 iOS 上,我错过了 kCTSuperscriptAttributeName 常量,但在字体大小和“基线”方面取得了不错的效果。对于不太听话的字体,它也为您提供了更多控制:

+ (NSAttributedString *)attributedStringForText:(NSString *)normalText andSuperscript:(NSString *)superscriptText textSize:(CGFloat)textSize
{
    UIFont *normalFont = [Styles mainFontWithSize:textSize];
    UIFont *superFont = [Styles mainFontWithSize:textSize / 2];

    NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc] initWithString:normalText attributes:@{NSFontAttributeName: normalFont}];

    NSAttributedString *superStr = [[NSAttributedString alloc] initWithString:superscriptText attributes:@{NSFontAttributeName: superFont, NSBaselineOffsetAttributeName:@(textSize/2)}];

    [finalStr appendAttributedString:superStr];

    return finalStr;
}       
于 2014-10-06T08:31:25.633 回答
0

如果你想让它更干净,你也可以执行以下操作

NSDictionary *attr = @{ NSFontAttributeName: smallfont, 
                        (NSString*)kCTSuperscriptAttributeName: @1 }

NSRange fabricWeightRange = NSMakeRange(fabricWeight.location + 2, 1);                   
[subKeyString setAttributes:attr range:fabricWeightRange];
于 2013-11-06T23:25:50.500 回答
0

对于下标,使用 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];
于 2015-08-27T06:30:04.637 回答