【问题标题】:-[__NSCFString _getValue:forType:]: unrecognized selector sent to instance-[__NSCFString _getValue:forType:]: 无法识别的选择器发送到实例
【发布时间】:2015-09-03 08:53:18
【问题描述】:
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:timeString];
    UIFont *font = [UIFont fontWithName:self.txtFieldForEndTime.font.familyName size:self.txtFieldForEndTime.font.pointSize / 2];

    NSDictionary *attrs = @{
                            NSFontAttributeName:font,
                            (__bridge NSString *)  kCTSuperscriptAttributeName:[string substringFromIndex:string.length - 2]
                            };
    [attString setAttributes:attrs range:[string rangeOfString:@"PM"]];

上面的代码正在尝试将最后两个 AM / PM 字符设置为超级脚本。

但应用程序通过声明 -[__NSCFString _getValue:forType:]: unrecognized selector sent to instance 而崩溃。

请告诉我这段代码有什么问题。如果您有上标 NSString 的一部分的工作代码,请告诉我。

【问题讨论】:

  • self.txtFieldForEndTime.font.familyName => self.txtFieldForEndTime.font.fontName?这可能是一个问题。另一个,是键kCTSuperscriptAttributeNameattrs 的值。它期待NSNumber,而不是NSString
  • 这是kCTSuperscriptAttributeName 的问题。字体工作正常。那么如何将任何字符显示为上标呢?
  • (__bridge NSString *) kCTSuperscriptAttributeName:[string substringFromIndex:string.length - 2](__bridge NSString *) kCTSuperscriptAttributeName:@(1)(__bridge NSString *) kCTSuperscriptAttributeName:@(-1) 如文档中所述。

标签: ios objective-c nsstring nsmutableattributedstring


【解决方案1】:

您可以通过减小字体的磅值来伪造上标,并使用NSBaselineOffsetAttributeName 属性将文本向上或向下移动基线。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多