【问题标题】:How to create a subscript with NSAttributedString如何使用 NSAttributedString 创建下标
【发布时间】:2023-03-07 19:38:01
【问题描述】:

我看到 NSAttributedString 有一个特定的上标属性,但我找不到下标的属性。使用 NSAttributedString 创建下标字符的一般做法是什么?

示例:H2O

【问题讨论】:

    标签: cocoa nsattributedstring


    【解决方案1】:

    尝试使用负值 NSSuperscriptAttributeName

    如果做不到这一点,最困难的方法是将字符串中的 [0123456789] 字符替换为 [₀₁₂₃₄₅₆₇₈₉]。

    【讨论】:

    • NSSuperscriptAttributeName 一起工作就像轻而易举!请注意,字体大小不会改变 - 只是 y 位置,下标必须显式变小(例如 NSFontAttributeName)。
    • NB - NSSuperscriptAttributeName 采用整数。这仅允许对您希望放置上标(或下标)的位置进行相当粗略的调整。我发现NSBaselineOffsetAttributeName 更容易用于更细粒度的控制..
    【解决方案2】:

    我曾为 NSMutableAttributedString 中的下标/上标苦苦挣扎了一段时间,因为最基本的解决方案需要为您想要下标的每个字符输入一个 NSRange。肯定有更自动化的做事方式吗?

    嗯,是的,但它需要一些工作。

    我的方法是在 NSString 中指示下标、上标、斜体等字符,方法是将要更改的文本用 % 符号括起来,然后是有关应进行哪种类型的字体调整的信息,例如'第二个粒子上的力由 f-下标-b 给出'在我的方案中会写成@“第二个粒子上的力由 f%&sb% 给出”。

    然后我使用方便的方法:

    NSArray *substrings = [string componentsSeparatedByString:@"%"];
    

    将字符串分割成由 % 符号分隔的子字符串,例如

    @"你好 %&B 你好吗?" -> 包含元素的数组:@"Hello",@"&Bhow are",@"you?"

    然后我检查数组中每个元素的第一个字符,看它是否包含 & 标记,我用它来表示下一个字符将是 B=Bold、S=Superscript、I=italics 等。

    因此,在上面的示例中,子字符串 @"&Bhow are" 旨在转换为粗体字符串 "how are" 和输入 @"Hello %&Bhow are% you ?”旨在转换为“Hello how are you?”。

    所有字体修改都使用 NSMutableAttributedString 及其相关方法执行,最后所有 NSMutableAttributedString 子字符串都可以使用诸如“appendAttributedString”之类的方法重新粘贴在一起。

    如果有人感兴趣,我的代码如下:

    -(void) appendFontString:(NSMutableAttributedString*) attribString
                             :(NSString*) string{
        NSArray *substrings = [string componentsSeparatedByString:@"%"];
        for(int i=0;i<(int) [substrings count];i++){
            if([substrings[i] length]>0){
            NSString* firstCharacter=[substrings[i] substringToIndex:1];
    
            if([firstCharacter isEqualToString:@"&"]){
                NSString* fontType=[substrings[i] substringWithRange:NSMakeRange(1, 1)];
    
                //remove first two characters
                NSString* newSubString=[substrings[i] substringFromIndex:2];
                if([fontType isEqualToString:@"S"]){
                [self appendWithSuperscript:attribString :newSubString];
                } else if([fontType isEqualToString:@"s"]){
                    [self appendWithSubscript:attribString :newSubString];
                } else if([fontType isEqualToString:@"B"]){
                    [self appendWithBold:attribString :newSubString];
                } else if([fontType isEqualToString:@"I"]){
                    [self appendWithItalics:attribString :newSubString];
                }
                } else{
                //regular string
                [self append:attribString :substrings[i]];
            }
            }
        }
    
    }
    

    其中 appendWithBold 等方法是用户创建的方法,可将 NSString 转换为格式化的粗体/上标/下标/等。 NSMutableAttributedString,然后将其附加到变量“attribString”。

    我的方法可能不是最好的,但至少它证明了通过一些工作,您可以在 Cocoa 中自动执行下标和上标。

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 2014-03-04
      • 2011-04-16
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多