【发布时间】:2013-11-05 20:50:05
【问题描述】:
我正在编写一个向后兼容到 iOS 6.0 的 iOS 应用程序。
在 iOS 7 中,NSString 实例方法 drawInRect:withAttributes: 替换了 drawInRect:withFont:lineBreakMode:alignment:。为了确定使用哪种方法,我有以下代码:
if ([NSString instancesRespondToSelector:@selector(drawInRect:withAttributes:)]) {
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[textStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[textStyle setAlignment:NSTextAlignmentLeft];
[[self title] drawInRect:_textRect withAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:FONT_SIZE], NSParagraphStyleAttributeName: textStyle}];
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[self title] drawInRect:_textRect withFont:[UIFont boldSystemFontOfSize:FONT_SIZE] lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];
#pragma clang diagnostic pop
}
由于drawInRect:withAttributes: 是在 iOS 7 中引入的,instancesRespondToSelector: 在早期版本的 iOS 上运行时应该返回 false。但是,在运行 iOS 6.1 的设备上测试它时,它返回 true,然后在尝试调用 drawInRect:withAttributes: 时崩溃。有谁知道发生了什么,或者我做错了什么?
【问题讨论】:
-
如果您测试 [[self title] respondsToSelector:@selector(drawInRect:withAttributes:)] 而不是使用方法“instancesRespondToSelector”,它将起作用。它在类别和类集群方面存在问题。
-
嗯,似乎没有修复它。仍然返回 true。
标签: ios objective-c ios6 nsstring ios7