21

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. Done googling, but found superscripts for A-Z, 0-9 characters, which is mentioned in unicode's site.

Sample code:

UILabel *myLabel; //do initialize stuff here

myLabel.text = @"My company\u00AE";

Thanks

illuminatus
  • 1,986
  • 4
  • 16
  • 19

7 Answers7

34

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/

Ahsan
  • 11,516
  • 12
  • 52
  • 79
sorin
  • 161,544
  • 178
  • 535
  • 806
  • thanx for the response, I too was thinking of html, was just checking if there is any alternate way out. – illuminatus Jun 01 '11 at 04:18
  • 3
    The problem is: (R) superscript or not is a font design issue. Use ® with a font that already "superscripts" ® (for instance Consolas) and you end up with an unreadable blob. – Mihai Nita Jun 23 '11 at 09:36
  • 3
    A good font that I use that shows the ® as a superscript is "Avenir Next" – Phamer Nov 07 '13 at 17:43
8

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
Integrating Stuff
  • 5,253
  • 2
  • 33
  • 39
1

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.

Jeehut
  • 20,202
  • 8
  • 59
  • 80
1

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
    }

}
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
0
<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.

unidentified-1
  • 131
  • 1
  • 7
0

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
Chris Allinson
  • 1,837
  • 2
  • 26
  • 39
0

Enclosing in a sup tag will work, but as someone commented above, a better solution might be to simply use a font that displays it as a superscript.

<span style="font-family:'Avenir Next">&reg;</span>

The benefit to this vs sup is that if the text is part of a hyperlink, for instance, the sup tag will interrupt the underline.

brassmookie
  • 104
  • 6