我想在表格单元格(UILabel 类型)中对齐日期,如下所示:
Monday 03 May
Tuesday 04 May
Wednesday 05 May
我尝试按如下方式使用属性字符串:
CTTextTabRef tabArray[] = { CTTextTabCreate(0, 12, NULL) }; // try 12 and see how it goes
CFArrayRef tabStops = CFArrayCreate( kCFAllocatorDefault, (const void**) tabArray, 1, &kCFTypeArrayCallBacks );
CTParagraphStyleSetting altSettings[] =
{
{ kCTParagraphStyleSpecifierTabStops, sizeof(CFArrayRef), &tabStops },
};
CTParagraphStyleRef style = CTParagraphStyleCreate( altSettings, sizeof(altSettings) / sizeof(CTParagraphStyleSetting) );
NSDictionary *dictionaryForATab = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge NSObject *)style, (NSString *)kCTParagraphStyleAttributeName, nil];
NSString *dateString = [self stringFromDate:date]; // returns something like this--> @"Monday \t 03 May"
cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:dateString attributes:dictionaryForATab];
但我得到一个
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType mutableCopyWithZone:]: unrecognized selector sent to instance 0x951b8e0'
错误。
关于如何解决它(或做同样事情的另一种方式)的任何想法?
(显然使用 Courier (或任何等宽字体)和空格填充工作日,不是一个有效的答案。我正在寻找更复杂的东西。)