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
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>®</sup>
You can check it at http://rishida.net/scripts/uniview/
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
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With