How to show superscript for “®” registered symbol?

前端 未结 6 2290
时光说笑
时光说笑 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 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 
    #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
    

提交回复
热议问题