2009-11-18 3 views
9

중간 수직 정렬로 표시하고자하는 NSTextFieldCell이 있습니다. 이전 질문과 블로그 항목 덕분에 저는 두 가지 해결 방법을 가졌습니다.NSTextFieldCell 수직 정렬, 솔루션 수평 정렬을 스쿼시 것 같다

그러나 두 솔루션 모두 셀을 오른쪽 정렬로 설정할 수있는 능력이 부족한 것처럼 보입니다. 누구나이 솔루션 중 하나를 두 가지 형태의 정렬을 모두 지원하도록 만들 수 있습니까? 대안 솔루션 (this blog에서 얻은)입니다

@implementation MiddleAlignedTextFieldCell 

- (NSRect)titleRectForBounds:(NSRect)theRect { 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 
    titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height)/2.0; 
    return titleFrame; 
} 

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    NSRect titleRect = [self titleRectForBounds:cellFrame]; 
    [[self attributedStringValue] drawInRect:titleRect]; 
} 

@end 

:

@implementation RSVerticallyCenteredTextFieldCell 

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 
    } 

    return newRect; 
} 

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength 
{ 
    aRect = [self drawingRectForBounds:aRect]; 
    mIsEditingOrSelecting = YES;  
    [super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength]; 
    mIsEditingOrSelecting = NO; 
} 

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent 
{ 
    aRect = [self drawingRectForBounds:aRect]; 
    mIsEditingOrSelecting = YES; 
    [super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent]; 
    mIsEditingOrSelecting = NO; 
} 

@end 

답변

5

작동하지 않기 때문에이 질문에 대한 답변을 게시하고 있습니다. 그러나 IB에서 정렬 설정을 확인하는 다른 방법을 찾을 수 없다는 사실은 매우 귀찮습니다. _cFlags에 액세스하는 것은 조금 더러운 것처럼 보입니다. 더 깨끗한 방법을 찾고 싶습니다.

앞서 게시 된 코드를 기반으로 this blog entry.

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    // Get the parent's idea of where we should draw 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 

     // For some reason right aligned text doesn't work. This section makes it work if set in IB. 
     // HACK: using _cFlags isn't a great idea, but I couldn't find another way to find the alignment. 
     // TODO: replace _cFlags usage if a better solution is found. 
     float widthDelta = newRect.size.width - textSize.width; 
     if (_cFlags.alignment == NSRightTextAlignment && widthDelta > 0) { 
      newRect.size.width -= widthDelta; 
      newRect.origin.x += widthDelta; 
     } 

    } 

    return newRect; 
} 
3

당신은 정렬을 설정 NSParagraphStyle/NSMutableParagraphStyle을 사용할 수 있습니다 (그리고 다른 속성 여기

은 하나 개의 솔루션에 대한 코드입니다). 특성이 설정된 문자열의 전체 범위에 적절하게 구성된 NSParagraphStyle 개체를 추가합니다.

+0

내 데이터 레이어로 프레젠테이션의 해당 부분을 연결해야하므로 솔직히 그 점을 좋아하지 않습니다. 이것은 IB에 제시된 옵션을 존중해야하지만, 어떤 이유에서든 수직 정렬은 IB의 선택 사항이어야한다. –

+0

NSTextFieldCell의 문자열을 모델 레이어에서 설정하는 것보다 더 혼합하는 방법을 모르겠습니다.:-) 정렬 (및 글꼴, 색상 및 크기, ...)은이 모든 것의 일부입니다. 그러나 수직 정렬은 옵션이어야한다고 동의합니다 - 개선 보고서를 제출하십시오. 사용할 수 없으므로 드로잉 코드에서이를 고려해야합니다. –

+0

어, 그게 "개선 요청"입니다. 링크 : http://bugreporter.apple.com –

2

제가 잠시 후에 물어 본 similar question에 몇 가지 잠재적 인 해결책이 게시되어 있습니다. 모든 정직에서

, 나는 아직 문서화되지 않은 _cFlags.vCentered 부울 사용 ( , 나쁜 프로그래머를!) 일을 얻을 수 있습니다. 간단하고 작동합니다. 나는해야만한다면 나중에 바퀴를 재발 명할 것이다.

갱신 :

OK, 나는 내가 그것을 알아 낸 것 같아요. 두 솔루션 모두 super을 호출하여 기본 rect를 얻은 다음 origin.ysize.height을 수정하여 수직 중심 맞춤을 수행합니다. 그러나 super을 호출하면 너비가 이미 텍스트에 맞게 조정 된 사각형이 가로로 반환됩니다.

용액 1 번 :

- (NSRect)titleRectForBounds:(NSRect)theRect { 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 

    // modified: 
    theRect.origin.y += (theRect.size.height - titleSize.height)/2.0 - 0.5; 
    return theRect; 
} 

용액 # 2에서는 그이 방법에 전달된다 RECT

용액 경계에서 origin.xsize.width를 사용하는 :

- (NSRect)drawingRectForBounds:(NSRect)theRect 
{ 
    NSRect newRect = [super drawingRectForBounds:theRect]; 

    // modified: 
    newRect.origin.x = theRect.origin.x; 
    newRect.size.width = theRect.size.width; 

    if (mIsEditingOrSelecting == NO) 
    { 
     // Get our ideal size for current text 
     NSSize textSize = [self cellSizeForBounds:theRect]; 

     // Center that in the proposed rect 
     float heightDelta = newRect.size.height - textSize.height; 
     if (heightDelta > 0) 
     { 
      newRect.size.height -= heightDelta; 
      newRect.origin.y += (heightDelta/2); 
     } 
    } 

    return newRect; 
} 
+0

사실 내 솔루션 중 하나를 찾은 그 질문에 대한 해결책이었습니다. –

+0

저는이 솔루션과 수평 정렬을 유지하는 방법을 알아 내려고하고 있습니다. 그러나 내가 할 수 없다면 내려 가서 다른 경로를 시도해야 할 수도 있습니다. –

+0

나는 수평 정렬이이 솔루션들 중 하나에 의해 영향을 받는다는 것에 놀랐습니다. 어떻게 정렬을하고 있니? –