NSTextFieldCell垂直对齐,解决方案似乎挤压水平对齐 [英] NSTextFieldCell vertical alignment, solutions seem to squash the horizontal alignment

查看:852
本文介绍了NSTextFieldCell垂直对齐,解决方案似乎挤压水平对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSTextFieldCell,我希望显示中间垂直对齐。感谢这里一个老问题和博客条目我有两个工作的解决方案。



但是,这两个解决方案似乎都压制了我将单元格设置为右对齐的能力。任何人都可以帮助我让这些解决方案支持这两种形式的对齐?



这里是一个解决方案的代码:

  @implementation MiddleAlignedTextFieldCell 

- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributStringValue] 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 attributStringValue] drawInRect:titleRect];
}

@end

此博客):

  @implementation RSVerticallyCenteredTextFieldCell 

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

if(mIsEditingOrSelecting == NO)
{
//获取我们理想的当前文本大小
NSSize textSize = [self cellSizeForBounds:theRect];

//在建议的矩形中居中
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: :(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是非常讨厌。访问_cFlags只是看起来有点脏,我很想找到一个更清洁的方法。



基于早期从此博客条目

   - (NSRect)drawingRectForBounds:(NSRect)theRect 
{
//获取父级的想法我们应该绘制
NSRect newRect = [super drawingRectForBounds:theRect];

if(mIsEditingOrSelecting == NO)
{
//获取我们理想的当前文本大小
NSSize textSize = [self cellSizeForBounds:theRect];

//在建议的矩形中居中
float heightDelta = newRect.size.height - textSize.height;
if(heightDelta> 0)
{
newRect.size.height - = heightDelta;
newRect.origin.y + =(heightDelta / 2);
}

//由于某些原因,右对齐文本不起作用。如果在IB中设置,此部分将使其工作。
// HACK:using _cFlags不是一个好主意,但我找不到另一种方法来找到对齐方式。
// TODO:如果找到更好的解决方案,请使用_cFlags用法。
float widthDelta = newRect.size.width - textSize.width;
if(_cFlags.alignment == NSRightTextAlignment&& widthDelta> 0){
newRect.size.width - = widthDelta;
newRect.origin.x + = widthDelta;
}

}

return newRect;
}


I have a NSTextFieldCell that I wish to display with middle vertical alignment. Thanks to an older question here and a blog entry I have two working solutions.

However, both solutions seem to squash my ability to set the cell as right aligned. Can anyone help me make either of these solutions support both forms of alignment?

Here is the code for one solution:

@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

The alternative solution is (obtained from this blog):

@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

解决方案

I'm posting this answer to the question since it does work, however, I find the fact that I couldn't find another way to check the alignment setting from IB is very annoying. Accessing _cFlags just seems a little dirty, and I'd love to find a cleaner method.

Based on the code posted earlier from 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;
}

这篇关于NSTextFieldCell垂直对齐,解决方案似乎挤压水平对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆