2010-12-17 3 views
12

나는 그것이 그려로 텍스트 주위에 테두리를 넣어 NSStrokeWidthAttributeNameNSAttributedString의 개체를 사용했습니다. 문제는 스트로크가 텍스트의 채우기 영역 내에 있음을 나타냅니다. 텍스트가 작 으면 (예를 들어, 1 픽셀 두께), 스트로킹은 텍스트를 읽기 어렵게 만듭니다. 내가 정말로 원하는 것은 바깥쪽에있는 뇌졸중입니다. 그것을 할 방법이 있습니까?NSAttributedString의 _outside_를 어떻게 획정합니까?

나는 아무 오프셋 및 흐림과 NSShadow을 시도했지만, 너무 흐릿하고 볼 어렵다. 어떤 흐림도없이 그림자의 크기를 늘릴 수있는 방법이 있다면 그렇게 할 수 있습니다.

답변

31

다른 방법이있을 수 있지만,이 작업을 수행하는 한 가지 방법은 먼저 직접 이전에 그려진 어떤 솟다 만 채우기 문자열을 그릴 만 스트로크 문자열을 그릴 것입니다. Adobe InDesign에는 실제로이 내장 기능이있어 문자 외부에 획을 적용하여 가독성을 높일 수 있습니다.

이 (http://developer.apple.com/library/mac/#qa/qa2008/qa1531.html 영감)이 작업을 수행하는 방법을 단지 예시 적이다 :

alt text :

@implementation MDInDesignTextView 

static NSMutableDictionary *regularAttributes = nil; 
static NSMutableDictionary *indesignBackgroundAttributes = nil; 
static NSMutableDictionary *indesignForegroundAttributes = nil; 

- (void)drawRect:(NSRect)frame { 
    NSString *string = @"Got stroke?"; 
    if (regularAttributes == nil) { 
     regularAttributes = [[NSMutableDictionary 
    dictionaryWithObjectsAndKeys: 
     [NSFont systemFontOfSize:64.0],NSFontAttributeName, 
     [NSColor whiteColor],NSForegroundColorAttributeName, 
     [NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName, 
     [NSColor blackColor],NSStrokeColorAttributeName, nil] retain]; 
    } 

    if (indesignBackgroundAttributes == nil) { 
     indesignBackgroundAttributes = [[NSMutableDictionary 
     dictionaryWithObjectsAndKeys: 
     [NSFont systemFontOfSize:64.0],NSFontAttributeName, 
     [NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName, 
     [NSColor blackColor],NSStrokeColorAttributeName, nil] retain]; 
    } 

    if (indesignForegroundAttributes == nil) { 
     indesignForegroundAttributes = [[NSMutableDictionary 
     dictionaryWithObjectsAndKeys: 
     [NSFont systemFontOfSize:64.0],NSFontAttributeName, 
     [NSColor whiteColor],NSForegroundColorAttributeName, nil] retain]; 
    } 

    [[NSColor grayColor] set]; 
    [NSBezierPath fillRect:frame]; 

    // draw top string 
    [string drawAtPoint: 
     NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 200.0) 
     withAttributes:regularAttributes]; 

    // draw bottom string in two passes 
    [string drawAtPoint: 
     NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 140.0) 
     withAttributes:indesignBackgroundAttributes]; 
    [string drawAtPoint: 
     NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 140.0) 
     withAttributes:indesignForegroundAttributes]; 
} 

@end 

이것은 다음과 같은 출력을 생성한다 : 첫째 속성을 설정

alt text

,363,210

지금, 그것은 확실히 기본보다 더 나은 모습, 그리 때로는 소수의 경계에 떨어질 것이기 때문에, 완벽하지,하지만. 성능에 문제가있는 경우

, 당신은 항상 같은있는 CoreGraphics 또는 CoreText로, 약간 낮은 수준까지 떨어로 볼 수 있었다.

+0

감사합니다. 치료를해라. –

+0

위대하고 상세한 답변. 시간을 가져 주셔서 감사합니다! – epologee

+1

간단합니다. 훌륭한. ... 그리고 완전히 불필요 할 것입니다 ... (지원은 이미 존재해야합니다) – u2Fan