【问题标题】:Degree Fahrenheit/Celsius Symbols ASCII NSString华氏度/摄氏度符号 ASCII NSString
【发布时间】:2023-03-03 18:25:01
【问题描述】:

如何在 NSString 中表示 华氏度 符号?下面的代码将产生一个度数符号,然后是大写字母 F,但是否有实际的华氏温度?同样,摄氏度是否有一个?

NSString *fahrenheit = [NSString stringWithFormat:@"%@F", @"\u00B0"];
NSString *celsius = [NSString stringWithFormat:@"%@C", @"\u00B0"];

【问题讨论】:

  • 为什么投反对票并关闭...
  • 为什么不直接使用符号呢?有 Unicode 字符:NSString *celcius = @"℃"; // U+2103 华氏温度相同。 Unicode 转义不需要字符串格式。
  • 没有。根据§22.2 of the Unicode standard,提供这些只是为了与旧编码兼容。 trojanfoe 的回答是正确的。

标签: ios objective-c nsstring


【解决方案1】:

您的代码是正确的,这就是您表示两个温度范围的方式。由于不需要使用stringWithFormat,因此可以稍微减少它:

NSString *fahrenheit = @"\u00B0F";
NSString *celsius = @"\u00B0C";

但您可以使用stringWithFormat 来格式化实际温度以及符号等:

float tempInFahrenheit = 23.4f;
float tempInCelsius = 56.8f;
NSString *fahrenheit = [NSString stringWithFormat:@"%f \u00B0F", tempInFahrenheit];
NSString *celsius = [NSString stringWithFormat:@"%f \u00B0C", tempInCelsius];

【讨论】:

    【解决方案2】:
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"80\u00b0c"];
            [attributedString setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica-light" size:40.0]
                                              , NSBaselineOffsetAttributeName : @22} range:NSMakeRange(2, 2)];
            //asign this as a 
    examplelabel.attributedtext = attributedString;
    

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      相关资源
      最近更新 更多