0

我有 UILabel,我希望每个说分成 3 列。我正在寻找第一个范围(NSRangeMake(0,5)是kCTLeftAlignment,第二个范围有kCTCenterAlignment,第三个范围有kCTRightAlignment。

我还没有使用过CoreText,所以我只是试图将(当前为NSTextAlignmentLeft)文本对齐更改为kCTRightAlignment,但我遇到了崩溃。

到目前为止,这是我的代码。UILabel 显示正确,没有属性文本等工作。我只是在寻找一些 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) 

我已经做了一些搜索和研究以达到这一点,但我被困住了。提前致谢!

4

2 回答 2

0

在我的情况下,我使用的是 aUILabel但它在这条线上崩溃了 CGSize expectedLabelSize = [headline sizeWithFont:headlineLabel.font constrainedToSize:maximumLabelSize lineBreakMode:headlineLabel.lineBreakMode];,我的崩溃显示了 UITextView[UITextView lineBreakMode]所以我只是UILabel在分配值之前初始化了我,它对我有用。可能对其他人有帮助。

于 2014-02-12T08:56:43.730 回答
0

首先,如果CTParagraphStyleCreate您使用 ARC 创建 CTParagraphStyle,则还必须释放该段落。所以之后[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;
}
于 2013-09-24T14:52:46.427 回答