2013-04-22 97 views
2

如果我在UILabel中有一段文字,我需要在文字中间显示注册商标,该如何实现?我真的不想使用图像视图。我有点想重写drawRect或者......但还没弄明白。如何在上标中提供注册商标

谢谢!

+4

使用Unicode? ™ - – 2013-04-22 14:35:32

+0

@MattBall,“™”是商标标志。注册*商标的符号是注册符号“®”U + 00AE。 – 2013-04-22 16:03:06

+0

@ JukkaK.Korpela我不清楚OP的实际含义。无论哪种方式,建议立场。 – 2013-04-22 16:10:55

回答

2

这是马特建议的unicode。这只是你如何在NSLocalizedString中使用它。 Unicode代表symbols。在处理字符串以支持使用语言文件的多种语言时,推荐使用NSLocalizedStrings。

someText.text = NSLocalizedString(@"iPhone\u2122",nil); 
+0

请解释。这是什么?为什么使用它? – Popeye 2013-04-22 14:39:08

+0

这是马特建议的unicode。这只是你如何在NSString中使用它。 http://en.wikipedia.org/wiki/List_of_Unicode_characters – 2013-04-22 14:40:11

+3

不需要'stringWithFormat'。请不要使用'stringWithFormat',除非字符串实际上具有需要用值替换的格式说明符。 – rmaddy 2013-04-22 14:41:09

5

可以使用内NSString任何地方的Unicode字符序列\u2122,它会适当地由UILabel呈现:

someLabel.text = @"This is some text about a Trademarked Name\u2122"; 
+5

或者你可以使这个可读并直接键入'?'字符。 'someLabel.text = @“这是一些关于商标名称的文字”;' – rmaddy 2013-04-22 14:43:06

2

使用性质注册SIGN“®” U + 00AE(书面内如果不能直接键入,则为\00AE)。请注意,它的外观因字体而异:一般来说,它看起来像上标,但这不是要求,有些字体在文本基线上显示,而“R”大约是常规“R”大小的一半,因此相当可读。

+0

谢谢,但我正在寻找一种方法以上标以编程方式显示符号 - 并非真的想要使用多个标签...... – Koolala 2013-05-06 14:07:32

2

从iOS6中我们可以使用NSAttributedString的概念。请参阅下面的代码片段。如果有人已经意识到这一点,请忽略。

static NSString *html = 
    @"<html>" 
    " <body>Here is trademark &reg; </i></body>" 
    "</html>"; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 280, 300, 200)]; 
    NSError *err = nil; 
    label.attributedText = 
    [[NSAttributedString alloc] 
    initWithData: [html dataUsingEncoding:NSUTF8StringEncoding] 
    options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } 
    documentAttributes: nil 
    error: &err]; 
    if(err) 
     NSLog(@"Unable to parse label text: %@", err);