我有一个关于显示注册符号作为上标的问题。我使用了Unicode值\u00AE,但它在同一行中显示。我想让它稍微顶部剩余的文本。
已经尝试搜索,但只找到了A-Z,0-9字符的上标,在Unicode网站中有提到。
示例代码:
UILabel *myLabel; //do initialize stuff here
myLabel.text = @"My company\u00AE";
感谢您。我有一个关于显示注册符号作为上标的问题。我使用了Unicode值\u00AE,但它在同一行中显示。我想让它稍微顶部剩余的文本。
已经尝试搜索,但只找到了A-Z,0-9字符的上标,在Unicode网站中有提到。
示例代码:
UILabel *myLabel; //do initialize stuff here
myLabel.text = @"My company\u00AE";
感谢您。Unicode没有上标形式的registered
符号,因此唯一的方法是使用HTML控件并将其包含在上标标签中:<sup>®</sup>
您可以在http://rishida.net/scripts/uniview/中检查它。
#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
对于新手来说,这是一个相对灵活的、Swift 4的解决方案,受到了Integrating Stuff原始答案的启发。我将它写成了NSAttributedString
的扩展 - 在我看来,这似乎是最清洁的解决方案。为了将(R)符号设置为上标,只需从调用此方法的任何位置传递字符串"®"即可。
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
}
}
如果你想要一个易于使用的 Swift 解决方案,你可以考虑查看 HandyUIKit。将其导入到你的项目中(例如通过 Carthage - 请参阅 README 中的说明),你可以像这样做:
import HandyUIKit
"My company^{®}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
subscripted(font:)
即可。它将识别像CO_{2}
这样的结构。如果您想要同时组合上下标,则可以使用superAndSubscripted(font:)
。
有关更多信息和其他示例,请参见文档。
在“集成东西”的答案中添加了一行,通过减小符号的字体大小(目前为硬编码值)来实现:
.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
使用sup标签包含可以起作用,但正如上面的评论所提到的,更好的解决方案可能是直接使用字体将其显示为上标。
<span style="font-family:'Avenir Next">®</span>
与 sup 标签相比,这种方法的好处在于,如果文本是超链接的一部分,sup 标签会打断下划线。
<div style="font-size:96px;">
Registered<span style="vertical-align:2.7em; font-size:0.2em;">®</span>
</div>
这些数字需要根据您的字体和字号进行微调。