How to show superscript for “®” registered symbol?

前端 未结 6 2291
时光说笑
时光说笑 2021-02-13 01:10

I\'ve a issue regarding showing registered symbol as superscript. I\'ve used unicode value \\u00AE, but it shows in same line. I\'d like to have it a bit top of remaining texts.

相关标签:
6条回答
  • 2021-02-13 01:50

    For a simple to use Swift solution, you might want to checkout HandyUIKit. After importing it into your project (e.g. via Carthage – see instructions in README) you can do something like this:

    import HandyUIKit
    
    "My company^{®}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
    

    This line will return an NSAttributedString which will look exactly like what you're looking for. Just assign it to a UILabels attributedText property and that's it!


    If you're looking for subscripting a text, simply use subscripted(font:) instead. It will recognize structures like CO_{2}. There's also superAndSubscripted(font:) if you want to combine both.

    See the docs for more information and additional examples.

    0 讨论(0)
  • 2021-02-13 01:51

    For newcomers, here is a relatively flexible, swift 4 solution, inspired by Integrating Stuff's original answer. I wrote it as an extension on NSAttributedString - that just seems like the cleanest solution in my mind. In order to superscript the (R) symbol, just pass the string "®" into this method from wherever you are calling it.

    extension NSAttributedString {
    
        class func superscriptInstances(ofString stringToReplace: String, withOriginalFont originalFont: UIFont, fromString string: String) -> NSAttributedString {
            let attributedString = NSMutableAttributedString(string: string)
            let length = attributedString.length
            let fontName = originalFont.fontName
            let fontSize = originalFont.pointSize
            let newSize = fontSize / 1.5
            let baselineOffset = fontSize / 3.0
            let newFont = UIFont(name: fontName, size: newSize)!
            var range = NSMakeRange(0, length)
            while (range.location != NSNotFound) {
                let nsstring = attributedString.string as NSString
                range = nsstring.range(of: stringToReplace, options: NSString.CompareOptions(rawValue: 0), range: range)
                if(range.location != NSNotFound) {
                    attributedString.addAttributes([.font: newFont,.baselineOffset: baselineOffset], range: range)
                    range = NSMakeRange(range.location + range.length, length - (range.location + range.length))
                }
            }
            return attributedString
        }
    
    }
    
    0 讨论(0)
  • 2021-02-13 01:54

    Added to "Integrating Stuff"'s answer by reducing the font size of the symbol (to a hard coded value for now):


    .h:

    #import <UIKit/UIKit.h>
    
    @interface UILabel (SetSuperScriptForRegisteredTrademarkSymbol) {
    
    }
    
    
    -(void)setSuperScriptForRegisteredTrademarkSymbol;
    
    
    @end
    

    .m:

    #import "UILabel+SetSuperScriptForRegisteredTrademarkSymbol.h"
    #import <CoreText/CTStringAttributes.h>
    
    
    @implementation UILabel (SetSuperScriptForRegisteredTrademarkSymbol)
    
    
    -(void)setSuperScriptForRegisteredTrademarkSymbol
    {
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    
        NSUInteger count = 0, length = [mutableAttributedString length];
        NSRange range = NSMakeRange(0, length);
    
        while(range.location != NSNotFound)
        {
            range = [[mutableAttributedString string] rangeOfString:@"®" options:0 range:range];
            if(range.location != NSNotFound)
            {
                [mutableAttributedString addAttribute: (NSString *)kCTSuperscriptAttributeName
                                                value: @"1"
                                                range: range];
    
                [mutableAttributedString addAttribute: NSFontAttributeName
                                                value: [UIFont systemFontOfSize:12.0]
                                                range: range];
    
                range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
    
                count++;
            }
        }
    
        self.attributedText = mutableAttributedString;
    }
    
    
    @end
    
    0 讨论(0)
  • 2021-02-13 02:02
    <div style="font-size:96px;">
    Registered<span style="vertical-align:2.7em; font-size:0.2em;">&reg;</span>
    </div>
    

    The numbers need tweaking depending on your font and point size.

    0 讨论(0)
  • 2021-02-13 02:15

    From iOS6 on, you can actually use a NSAttributedString with a UILabel.

    To set superscript for the registered trademark symbol, you can use the following category:

    #import <CoreText/CTStringAttributes.h>
    #import "UILabel+ SetSuperScriptForRegisteredTrademarkSymbol.h"
    
    @implementation UILabel (SetSuperScriptForRegisteredTrademarkSymbol)
    
    - (void) setSuperScriptForRegisteredTrademarkSymbol {
    
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    
        NSUInteger count = 0, length = [mutableAttributedString length];
        NSRange range = NSMakeRange(0, length);
    
        while(range.location != NSNotFound)
        {
            range = [[mutableAttributedString string] rangeOfString:@"®" options:0 range:range];
            if(range.location != NSNotFound) {
                [mutableAttributedString addAttribute:(NSString*)kCTSuperscriptAttributeName value:@"1" range:range];
                range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
                count++;
            }
        }
    
        self.attributedText = mutableAttributedString;
    }
    
    @end
    
    0 讨论(0)
  • 2021-02-13 02:16

    Unicode does not have a registered symbol in superscript form so the only way to do it is to use a HTML control and to include it into superscript tags: <sup>&reg;</sup>

    You can check it at http://rishida.net/scripts/uniview/

    0 讨论(0)
提交回复
热议问题