Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show superscript for "®" registered symbol?

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

like image 793
illuminatus Avatar asked May 31 '11 14:05

illuminatus


4 Answers

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/

like image 103
sorin Avatar answered Nov 16 '22 06:11

sorin


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
like image 22
Integrating Stuff Avatar answered Nov 16 '22 06:11

Integrating Stuff


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.

like image 1
Jeehut Avatar answered Nov 16 '22 04:11

Jeehut


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
    }

}
like image 1
Brian Sachetta Avatar answered Nov 16 '22 05:11

Brian Sachetta