سؤال

لدي مشكلة صغيرة مع NstableView. عندما أقوم بزيادة ارتفاع صف في الجدول ، يتم محاذاة النص الموجود فيه في أعلى الصف ولكني أريد محاذاة تركز عليه رأسياً!

هل يمكن لأي شخص أن يقترحني بأي طريقة للقيام بذلك ؟؟

شكرًا،

ميرااج

هل كانت مفيدة؟

المحلول

هذا هو حل رمز بسيط يوضح الفئة الفرعية التي يمكنك استخدامها لمحاذاة Middle the TextFieldCell.

رأس

#import <Cocoa/Cocoa.h>


@interface MiddleAlignedTextFieldCell : NSTextFieldCell {

}

@end

الرمز

@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

إدخال المدونة هذا يظهر حل بديل يعمل أيضًا بشكل جيد.

نصائح أخرى

فيما يلي النسخة السريعة من بناء الكود على الإجابة أعلاه:

import Foundation
import Cocoa

class VerticallyCenteredTextField : NSTextFieldCell
{

    override func titleRectForBounds(theRect: NSRect) -> NSRect
    {
        var titleFrame = super.titleRectForBounds(theRect)
        var titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize.height) / 2.0
        return titleFrame
    }

    override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
    {
        var titleRect = self.titleRectForBounds(cellFrame)

        self.attributedStringValue.drawInRect(titleRect)
    }
}

ثم قمت بتعيين ارتفاع جدول TableView في NstableView:

func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
    return 30
}

قم بتعيين فئة NSTextfieldCell لتكون رأسياً فيردة:

enter image description here

وارتفاع TableViewCell

enter image description here

enter image description here

شكرا برايان لمساعدتكم.

تم تحديث إجابة @iphaaw لـ Swift 4 (ملاحظة أنا أيضًا أضفت "الخلية" في نهاية اسم الفصل للوضوح ، والذي يحتاج أيضًا إلى مطابقة اسم الفصل في Builder):

import Foundation
import Cocoa

class VerticallyCenteredTextFieldCell : NSTextFieldCell {
    override func titleRect(forBounds theRect: NSRect) -> NSRect {
        var titleFrame = super.titleRect(forBounds: theRect)
        let titleSize = self.attributedStringValue.size
        titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize().height) / 2.0
        return titleFrame
    }

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
        let titleRect = self.titleRect(forBounds: cellFrame)
        self.attributedStringValue.draw(in: titleRect)
    }
}

حتى لو كان هذا سؤالًا قديمًا للغاية مع إجابة مقبولة ، فإليك حلًا بديلًا:

انتقل إلى IB ، حدد NSTextfield الذي يجلس في NstableCellView وأضف عائقًا جديدًا "مركزًا رأسياً في الحاوية". يجب عليك أيضًا إضافة قيود أفقية (والتي من المحتمل أن تكون رائدة/متخلف ، تم تعيين المساحة إلى 0 أو أي شيء يناسب احتياجاتك).

استخدم هذا في nsoutlineview وعمل مثل سحر. لم يكن الأداء مشكلة في حالتي ، حيث لم يكن لدي العديد من الخلايا ، لكنني أتوقع أن يكون الأمر أسوأ من حساب الأحجام يدويًا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top