componentkit - 如何使用CKStackLayoutComponent水平对齐两个标签?

标签 componentkit

我刚刚开始使用 ComponentKit,在水平对齐 2 个标签时遇到一些困难。 我希望第一个靠近左边距,第二个靠近右边距。

通过自动布局,我可以通过这组约束来做到这一点:

H:|-0-[_label1]-[_label2]-0-|

我尝试的一切似乎都不起作用,我总是得到相同的结果:两个标签左对齐。

这是 super 简单的组件:

+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
  return [super
          newWithComponent:
          [CKStackLayoutComponent
           newWithView:{}
           size:{}
           style:{
             .direction = CKStackLayoutDirectionHorizontal,
             .alignItems = CKStackLayoutAlignItemsCenter
           }
           children:{
             {
               [CKLabelComponent
                newWithLabelAttributes:{
                  .string = text1,
                  .font = [UIFont systemFontOfSize:20],
                  .color = [UIColor blackColor]
                }
                viewAttributes:{
                  {@selector(setBackgroundColor:), [UIColor clearColor]}
                }],
               .alignSelf = CKStackLayoutAlignSelfStretch
             },
             {
               [CKLabelComponent
                newWithLabelAttributes:{
                  .string = text2,
                  .font = [UIFont systemFontOfSize:20],
                  .color = [UIColor redColor],
                  .alignment = NSTextAlignmentRight
                }
                viewAttributes:{
                  {@selector(setBackgroundColor:), [UIColor clearColor]}
                }],
               .alignSelf = CKStackLayoutAlignSelfStretch
             }
           }]];
}

如果有人有任何建议,我们将不胜感激。

最佳答案

伟大的发现——这是我们的 Flexbox 实现 (CKStackLayoutComponent) 中的一个缺点。您正在寻找与 justify-content: space- Between; 等效的内容,但我们尚不支持它。

如果您想向堆栈布局实现提交补丁来支持这一点,我们将不胜感激。否则,我会尝试找人提供支持。

现在,您可以通过使用 flexGrow = YES 放入垫片来伪造它:

+ (instancetype)newWithText1:(NSString *)text1 text2:(NSString *)text2
{
  return [super
          newWithComponent:
          [CKStackLayoutComponent
           newWithView:{}
           size:{}
           style:{
             .direction = CKStackLayoutDirectionHorizontal,
             .alignItems = CKStackLayoutAlignItemsCenter
           }
           children:{
             {
               [CKLabelComponent
                newWithLabelAttributes:{
                  .string = text1,
                  .font = [UIFont systemFontOfSize:20],
                  .color = [UIColor blackColor]
                }
                viewAttributes:{
                  {@selector(setBackgroundColor:), [UIColor clearColor]}
                }],
               .alignSelf = CKStackLayoutAlignSelfStretch
             },
             {
              [CKComponent new],
              .flexGrow = YES,
             },
             {
               [CKLabelComponent
                newWithLabelAttributes:{
                  .string = text2,
                  .font = [UIFont systemFontOfSize:20],
                  .color = [UIColor redColor],
                  .alignment = NSTextAlignmentRight
                }
                viewAttributes:{
                  {@selector(setBackgroundColor:), [UIColor clearColor]}
                }],
               .alignSelf = CKStackLayoutAlignSelfStretch
             }
           }]];
}

关于componentkit - 如何使用CKStackLayoutComponent水平对齐两个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460148/

相关文章:

ios - 如何配置组件的底层?

ios - 如何将 UIGestureRecognizerDelegate 分配给 CKComponentController

ios - CKComponent 处理点击手势

ios - 如何为具有多个参数的方法创建CKComponentViewAttribute

ios - ComponentKit 在子类化 CKCompositeComponent 后无法调用 super newWithView