我有关于显示注册符号为上标的问题。我使用了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>
<div style="font-size:96px;">
Registered<span style="vertical-align:2.7em; font-size:0.2em;">®</span>
</div>
检查
这些数字需要根据您的字体和磅值进行调整。
从iOS6开始,实际上可以使用带UILabel的NSAttributedString。
要为注册商标符号集标,您可以使用以下类别:
#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
很酷!!!!! – 2015-07-24 18:51:14
加入到通过降低符号的字体大小(以硬编码值“集成的东西”的答案现在):
.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
对于使用简单迅速解决,你可能要签HandyUIKit。将它导入到你的项目后(通过迦太基例如 - 见README说明),你可以做这样的事情:
import HandyUIKit
"My company^{®}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium))
这条线将返回一个NSAttributedString
将看起来就像你正在寻找。只需将它分配到UILabel
s attributedText
财产和就是这样!
如果您正在寻找下标文本,只需使用subscripted(font:)
代替。它将识别像CO_{2}
这样的结构。还有superAndSubscripted(font:)
,如果你想结合都。
有关更多信息和其他示例,请参阅docs。
thanx的回应,我也在想html,只是检查是否有任何替代方式。 – illuminatus 2011-06-01 04:18:02
问题是:(R)上标与否是字体设计问题。使用®,其字体已经“上标”®(例如Consolas),并且最终会出现不可读的blob。 – 2011-06-23 09:36:30
我使用的一个很好的字体显示®作为上标是“Avenir Next” – Phamer 2013-11-07 17:43:21