我有關於顯示註冊符號爲上標的問題。我使用了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