Выравнивание по вертикали NSTextFieldCell, решения, кажется, давят выравнивание по горизонтали

Проблема

стратегическая модель используется для решения проблем, которые могли бы (или предсказываться, они могли бы) быть реализованным или решенным различным стратегии и которые обладают ясно определенным интерфейсом для таких случаев. Каждая стратегия совершенно действительна самостоятельно с некоторыми стратегиями, являющимися предпочтительным в определенных ситуациях, которые позволяют приложению переключаться между ними во время времени выполнения.

Пример кода

namespace StrategyPatterns
{
  // Interface definition for a Sort algorithm
  public interface ISort
  {
    void Sort(List<string> list)
  }

  // QuickSort implementation
  public class CQuickSorter : ISort
  {
    void Sort(List<string> list)
    {
      // Here will be the actual implementation
    }
  }

  // BubbleSort implementation
  public class CBubbleSort : ISort
  {
    void Sort(List<string> list)
    {
      // The actual implementation of the sort
    }
  }

  // MergeSort implementation
  public class CMergeSort : ISort
  {
    void Sort(List<string> list)
    {
      // Again the real implementation comes here
    }
  }

  public class Context
  {
    private ISort sorter;

    public Context(ISort sorter)
    {
      // We pass to the context the strategy to use
      this.sorter = sorter;
    }

    public ISort Sorter
    {
      get{return sorter;)
    }
  }

  public class MainClass
  {
    static void Main()
    {
       List<string> myList = new List<string>();

       myList.Add("Hello world");
       myList.Add("Another item");
       myList.Add("Item item");

       Context cn = new Context(new CQuickSorter());
       // Sort using the QuickSort strategy
       cn.Sorter.Sort(myList);
       myList.Add("This one goes for the mergesort");
       cn = new Context(new CMergeSort());
       // Sort using the merge sort strategy
       cn.Sorter.Sort(myList);
    }
  }
}
9
задан Bryan McLemore 18 November 2009 в 21:51
поделиться

3 ответа

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;
}
5
ответ дан 4 December 2019 в 21:10
поделиться

Вы можете использовать NSParagraphStyle / NSMutableParagraphStyle, чтобы установить выравнивание (и другие атрибуты). Добавьте должным образом сконфигурированный объект NSParagraphStyle ко всему диапазону вашей атрибутированной строки.

3
ответ дан 4 December 2019 в 21:10
поделиться

Есть несколько потенциальных решений, опубликованных в аналогичном вопросе , который я задал некоторое время назад.

Честно говоря, я все еще использую недокументированное _cFlags.vCentered boolean ( ] tsk tsk , плохой программист!), чтобы выполнить работу. Это просто, и это работает. Я' Позже я изобрету велосипед, если понадобится.

update:

Хорошо, я думаю, что разобрался. Оба решения полагаются на вызов super для получения прямоугольника по умолчанию, а затем на изменение origin.y и size.height для выполнения вертикального центрирования. Однако вызовы super возвращают прямоугольник, ширина которого уже была скорректирована для соответствия тексту по горизонтали.

Решение состоит в использовании origin.x и size .width из прямоугольника границ, который передается в метод:

В решении №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:

- (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;
}
2
ответ дан 4 December 2019 в 21:10
поделиться