如何使用 NSAttributedString 制作下标和上标?

发布于 2024-12-21 22:55:59 字数 92 浏览 2 评论 0原文

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

这是否可以与 NSAttributedString 相关,或者是否有其他/更简单的方法来制作下标?

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 subscripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆梦 2024-12-28 22:55:59

这是我在 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];

我希望这对其他人有帮助,那里的示例并不多!

Here's what I did in iOS 6. First add the CoreText, and QuartzCore frameworks. Then import:

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

I made a small function that inputs a plain NSString and exports a NSMutableAttributedString with the last character in superscript. This can be modified to allow setting superscript or subscript, change kCTSuperscriptAttributeName value to -1. Also you could add a variable to specify where to put the superscript in the string. Right now it just assumes the end of the string.

- (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;
}

Now when I want to use it I can do the following to put it in a UITextField:

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

I hope this helps somebody else, there's not many examples out there!

青衫负雪 2024-12-28 22:55:59

这可以通过 NSAttributedString 来完成。您要查找的属性常量取决于您的平台。对于 Mac OS X,它是 NSSuperscriptAttributeName,在 iOS 上它是 kCTSuperscriptAttributeName。为下标传入负值。

唯一需要注意的是,iOS 上的 UILabel 无法绘制 NSAttributedString(不过,请为 iOS 6 祈祷)。您需要使用 Core Text 绘制文本,或者找到可以绘制 NSAttributedStringUILabel 的第三方替代品。

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.

千柳 2024-12-28 22:55:59

在 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;
}       

On iOS, I had missed the kCTSuperscriptAttributeName constant but had good results with font size and "baseline". It gives you a little more control too for less obedient fonts:

+ (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;
}       
网白 2024-12-28 22:55:59

对于 SubScript,kCTSuperscriptAttributeName 的使用值为 @-1。

根据文档

@discussion 值必须是 CFNumberRef。默认值为 int 值 0。如果
支持
通过指定的字体,值为 1 启用上标,值为 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];

For SubScript use value for kCTSuperscriptAttributeName as @-1.

As per the document

@discussion Value must be a CFNumberRef. Default is int value 0. If
supported
by the specified font, a value of 1 enables superscripting and a
value of -1 enables subscripting.

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];
稀香 2024-12-28 22:55:59

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

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

NSRange fabricWeightRange = NSMakeRange(fabricWeight.location + 2, 1);                   
[subKeyString setAttributes:attr range:fabricWeightRange];

you can also do the following if you want to make it a litle cleaner

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

NSRange fabricWeightRange = NSMakeRange(fabricWeight.location + 2, 1);                   
[subKeyString setAttributes:attr range:fabricWeightRange];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文