我有UILabel,我希望每个说法分成3列。 我希望第一个范围(NSRangeMake(0,5)是kCTLeftAlignment,第二个范围是kCTCenterAlignment,第三个范围是kCTRightAlignment。
我还没有使用过CoreText,所以我只是想将(当前的NSTextAlignmentLeft)文本对齐方式更改为kCTRightAlignment,但是我遇到了崩溃。
到目前为止,这是我的代码。 UILabel显示正常,没有attributesText等工作。我只是在寻找一些NSAttributedString帮助。
- (void)setTitle:(NSString *)_title {
titleLabel.text = _title;
NSMutableAttributedString * attr = [[NSMutableAttributedString alloc] initWithString:_title];
CTTextAlignment theAlignment = kCTRightTextAlignment;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] = {{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment }};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)theParagraphRef, (id)kCTParagraphStyleAttributeName, nil];
[attr addAttributes:attributes range:NSMakeRange(0, _title.length)];
titleLabel.attributedText = attr;
}
这是错误/崩溃
2013-01-24 13:05:59.928 Expandable[12376:c07] -[__NSCFType lineBreakMode]: unrecognized selector sent to instance 0x716f1b0
2013-01-24 13:05:59.929 Expandable[12376:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType lineBreakMode]: unrecognized selector sent to instance 0x716f1b0'
*** First throw call stack:
(0x157c012 0x13a1e7e 0x16074bd 0x156bbbc 0x156b94e 0x33ebea1 0x33eba56 0x446c24 0x4452e9 0x4476a8 0x3394be 0x203a3f 0x20396b 0x115697 0x20383c 0x2039ba 0x2032b6 0x203994 0x1f80e2 0x1f815c 0x1760bc 0x177227 0x1778e2 0x1544afe 0x1544a3d 0x15227c2 0x1521f44 0x1521e1b 0x19f87e3 0x19f8668 0x2e965c 0x209d 0x1fc5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
我做了一些搜索和研究以实现这一目标,但我陷入了困境。 提前谢谢!
答案 0 :(得分:0)
首先,如果您使用CTParagraphStyleCreate
创建CTParagraphStyle,则必须在使用ARC时释放该段落。
因此,在[attr addAttributes:attributes range:NSMakeRange(0, _title.length)];
之后,您必须致电CFRelease(theParagraphRef);
。
要修复崩溃,你必须检查是否存在NSMutableParagraphStyle类(可从iOS 6获得)并使用它。
您的代码应如下所示:
- (void)setTitle:(NSString *)_title {
titleLabel.text = _title;
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:_title];
if ([NSMutableParagraphStyle class]) {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentRight;
[attr addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, _title.length)];
}
else {
CTTextAlignment theAlignment = kCTRightTextAlignment;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] = {{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &theAlignment }};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)theParagraphRef, (id)kCTParagraphStyleAttributeName, nil];
[attr addAttributes:attributes range:NSMakeRange(0, _title.length)];
CFRelease(theParagraphRef);
}
titleLabel.attributedText = attr;
}
答案 1 :(得分:0)
在我的情况下,我使用的是UILabel
,但它在CGSize expectedLabelSize = [headline sizeWithFont:headlineLabel.font constrainedToSize:maximumLabelSize lineBreakMode:headlineLabel.lineBreakMode];
行崩溃了,我的崩溃显示了UITextView [UITextView lineBreakMode]
所以我刚刚初始化了我的UILabel
分配价值,它对我有用。可能对其他人有帮助。