2013-01-24 23 views
0

我有UILabel,我希望分成每列3列。 我期待有第一個範圍(NSRangeMake(0,5)爲kCTLeftAlignment,第二個範圍爲kCTCenterAlignment,第三個爲kCTRightAlignment。3在一個UILabel上單獨的文本對齊

我還沒有使用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

一個UILabel的不同範圍不可能有單獨的路線。你打算使用三種不同的UILabel? –

+0

我曾經希望我不必這樣做。使用CoreText繪製文本可能會更高效。它在一個自定義tableviewcell中。 –

回答

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; 
} 
0

在我來說,我使用的是UILabel但它墜毀在這一行CGSize expectedLabelSize = [headline sizeWithFont:headlineLabel.font constrainedToSize:maximumLabelSize lineBreakMode:headlineLabel.lineBreakMode];和我撞車事故中顯示有關的UITextView [UITextView lineBreakMode],所以我剛剛分配之前初始化我UILabel價值和它爲我工作。可能會對別人有幫助。