首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CKStackLayoutComponent水平对齐两个标签?

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

Stack Overflow用户
提问于 2015-04-05 17:31:30
回答 1查看 671关注 0票数 2

我刚刚开始玩ComponentKit,我在水平排列两个标签时遇到了一些困难。我要第一个靠近左边边缘,第二个靠近右边。

有了自动布局,我可以使用这组约束来完成它:

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

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

这是超级简单的组件:

代码语言:javascript
复制
+ (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
             }
           }]];
}

如果有人有什么建议会很感激的话。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-05 18:00:33

伟大的发现-这是我们的柔性盒实现(CKStackLayoutComponent)的一个缺点。您正在寻找类似于justify-content: space-between;的内容,但我们还不支持它。

如果您想向堆栈布局实现提交一个补丁以支持这一点,我们将不胜感激。否则,我会设法找一个人来增加支持。

现在,您可以通过使用flexGrow =YES添加一个间隔来伪造它:

代码语言:javascript
复制
+ (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
             }
           }]];
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29460148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档