2012-04-25 5 views
1

하나의 열에 파일 경로를 보여주는 NSTableView가 있습니다. 사용자가 tableview의 크기를 조정할 때 경로 이름 (예 : /Users/name/testfile.m)의 크기를 조정하고 싶지만 경로 이름의 끝 (예 : ... name/testfile.m)을 표시하고 싶습니다. 디폴트로 발생하는 경로의 시작 (예 :/Users/test/te ...). 내가하고 싶은 일을 성공적으로 수행하는 함수를 작성했지만, 사용자가 tableview의 크기를 조절할 때 다시 그리는 동안 tableview가 깜빡입니다. 이 일을 더 나은, 더 우아한 알고리즘이 있어야합니다 생각하지만 NSString 및 Stackoverflow에 대한 설명서를 조사하고 더 나은 솔루션을 제공하는 아무것도 찾을 수 없습니다. 누구든지이 문제에 대한보다 우아한 해결책을 가지고 있다면 크게 감사 할 것입니다. 감사! 건배, 트론문자열을 자릅니다.

나의 현재 기능 :

-(NSString *) truncateString:(NSString *) myString withFontSize:(int) myFontSize withMaxWidth:(NSInteger) maxWidth 
{ 
    // Get the width of the current string for a given font 
    NSFont *font = [NSFont systemFontOfSize:myFontSize]; 
    CGSize textSize = NSSizeToCGSize([myString sizeWithAttributes:[NSDictionary  dictionaryWithObject:font forKey: NSFontAttributeName]]); 
    NSInteger lenURL =(int)textSize.width; 

    // Prepare for new truncated string 
    NSString *myStringShort; 
    NSMutableString *truncatedString = [[myString mutableCopy] autorelease]; 

    // If the available width is smaller than the string, start truncating from first character 
    if (lenURL > maxWidth) 
    { 
     // Get range for first character in string 
     NSRange range = {0, 1}; 

     while ([truncatedString sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey: NSFontAttributeName]].width > MAX(TKstringPad,maxWidth)) 
     { 
      // Delete character at start of string 
      [truncatedString deleteCharactersInRange:range]; 
     }  
     myStringShort = [NSString stringWithFormat:@"...%@",truncatedString]; 
    } 
    else 
    { 
     myStringShort=myString; 
    } 
    return myStringShort; 
} 

답변

5

일반적인 접근 방식은 단순히 다음과 같습니다

[tableViewCell setLineBreakMode:NSLineBreakByTruncatingHead]; 

Dondragmer가 언급 한 바와 같이,이 속성은 엑스 코드의 NIB 편집기에서 설정할 수 있습니다.

+2

또한 인터페이스 작성기에서 열을 비롯한 셀을 선택하고 거기에서 줄 바꿈 모드를 변경하여이 작업을 수행 할 수 있습니다. 동일한 효과가 방금 다른 장소에 적용되었습니다. – Dondragmer

+0

@Dondragmer +1 예. 덧붙였다. 감사. – justin

+0

@ Justin : 답장을 보내 주셔서 감사합니다. 나는 당신의 제안을 따르려고했으나 효과가 없었습니다. 그것은 내가 자신의 NSCell을 가지고 있기 때문일 수 있습니다. 여기서'- (void) drawWithFrame : (NSRect) cellFrame inView : (NSView *) controlView'를 오버라이드하여 각 tablecolumn에 텍스트를 그립니다. 또한 변경없이 IB에 설정하려고했습니다. 어떤 제안? 감사! –

관련 문제