중간 수직 정렬로 표시하고자하는 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
내 데이터 레이어로 프레젠테이션의 해당 부분을 연결해야하므로 솔직히 그 점을 좋아하지 않습니다. 이것은 IB에 제시된 옵션을 존중해야하지만, 어떤 이유에서든 수직 정렬은 IB의 선택 사항이어야한다. –
NSTextFieldCell의 문자열을 모델 레이어에서 설정하는 것보다 더 혼합하는 방법을 모르겠습니다.:-) 정렬 (및 글꼴, 색상 및 크기, ...)은이 모든 것의 일부입니다. 그러나 수직 정렬은 옵션이어야한다고 동의합니다 - 개선 보고서를 제출하십시오. 사용할 수 없으므로 드로잉 코드에서이를 고려해야합니다. –
어, 그게 "개선 요청"입니다. 링크 : http://bugreporter.apple.com –