2014-09-24 5 views
3

더 이상 사용되지 않는 API를 사용하지 않고 그림자로 그릴 다중 선 텍스트를 얻으려고합니다. 그것은 한 줄에 잘 작동합니다.다중 선 nsstring에서 그림자를 얻을 수 없습니다.

-(void)drawRect:(CGRect)rect 
{ 
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; 
    paragraph.lineBreakMode = NSLineBreakByWordWrapping; 
    paragraph.alignment = NSTextAlignmentCenter; 

    UIFont *f = [UIFont systemFontOfSize:20.0]; 
    NSMutableDictionary *attributes = [NSMutableDictionary new]; 
    [attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f, 
                NSParagraphStyleAttributeName : paragraph, 
                NSForegroundColorAttributeName : [UIColor blueColor] }]; 
    NSShadow * shadow = [NSShadow new]; 
    shadow.shadowOffset = CGSizeMake(4,4); 
    shadow.shadowColor = [UIColor redColor]; 

    [attributes setValue:shadow forKey:NSShadowAttributeName]; 

    rect.origin.y = 100; 
    [@"test string on one line" drawInRect:rect withAttributes:attributes]; 

    rect.origin.y = 150; 
    [@"test string spanning more than one line" drawInRect:rect withAttributes:attributes]; 
} 

을 출력은 다음과 같습니다 : 관련 코드는 다음과 같습니다 나는 아이폰 5 (7.1.2), 아이폰 6 (8.0), 건물에 검사를하지

iPhone 6 showing no shadow on multiline text

또한 xCode 5로 빌드 할 때 iPhone 5에서 테스트했습니다.

+0

지옥, 나는 두 개의 동일한 라벨을 사용했을 것입니다. 하나는 다른 하나를 오버레이하고 오프셋은 투명 배경입니다. –

+0

그건 0이 아닌 shadowBlurRadius에서 작동하지 않을까요? –

답변

6

더 많은 실험을 통해 NSAttributedString을 사용하는 것으로 나타났습니다.

NSString *s = @"test string spanning more than one line" 
    [s drawInRect:rect withAttributes:attributes] 

이 수행합니다 :이 그림자 표시되지 않지만

NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:attributes]; 
    [as drawInRect:rect]; 

그렇게하지를 그렇지 않으면 듣고 싶어요이 어디 설명되어 생각합니다.

+0

OS X의 최초 출시 후 16 년이 지나면 NSAttributedString과 그 속성에 대한 적절한 문서를 작성해야한다고 생각해야합니다. 아니. BTW, 당신은 저 한테 많은 번거 로움을 덜어 줬습니다. –

관련 문제