c++ - 如何右对齐第一个组件

标签 c++ ios objective-c componentkit

我正在尝试实现以下布局:

     Name: James
   Gender: Male
Followers: Brian, Jason and Mike.

我可以想象顶层是使用 CKStackLayout,但是对于每个条目,例如“名字:James”,如何让名字在一个 Box 中右对齐?

我正在使用类似下面的内容,将文本包装在一个固定宽度的组件中,并将标签组件设置为使用 NSTextAlignmentRight 对齐方式,但它不起作用,左右都只是 '左对齐':

static CKComponent *singleLine(NSString *left, NSString *right)
{
  CKStackLayoutComponent *stackComponent =
  [CKStackLayoutComponent
   newWithView:{}
   size:{}
   style:{
     .direction = CKStackLayoutDirectionHorizontal,
     .alignItems = CKStackLayoutAlignItemsStretch,}
       children:{
         {.component = [CKStaticLayoutComponent 
                        newWithView:{}
                        size:{.width = CKRelativeDimension(10)}]},
         {.component =
           [CKStaticLayoutComponent
            newWithView:{}
            size:{.width = CKRelativeDimension(88)}
            children:{
            {
            .component =
            [CKLabelComponent
             newWithLabelAttributes:{
               .string = left,
               .alignment = NSTextAlignmentRight,
               .font = [UIFont boldSystemFontOfSize:14.0],
               .maximumNumberOfLines = 1
             }
             viewAttributes:{}],
          }
        }],
     },
     {
       .component =
       [CKLabelComponent
        newWithLabelAttributes:{
          .string = right,
          .alignment = NSTextAlignmentLeft,
          .font = [UIFont systemFontOfSize:14.0],
          .maximumNumberOfLines = 1
        }
        viewAttributes:{}],
       .spacingBefore = 10.0
     }
   }];

  return stackComponent;
}

谢谢!

最佳答案

我认为您的解决方案很好。另一种解决方案是通过覆盖 computeLayoutThatFits: 编写您自己的自定义布局。通常最好避免这种情况,但对于像表单这样的复杂布局,它可以成为救星。

关于c++ - 如何右对齐第一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288875/

相关文章:

c++ - 包含 '?' 的 QNetworkRequest URL - 由于 QUrl 编码而无法正确请求

c++ - iOS:将 Qt 添加到现有的 Xcode 项目

iphone - isOrientationSupported 在 IOS 中已弃用

ios - 如何获取与其他不使用 id 字段相关的托管对象?

c++ - boost::this_thread::sleep() 与 nanosleep()?

iphone - UIButtons 位于 UIView 下方,造成干扰

iphone - 在 UITableView 中对大量行/节进行动画处理性能不佳

ios - 如何解决 UIWebView 中的 "Frame Load Interrupted"错误?

ios - 按下 UIButton 时运行函数

c# - 如何将 SWIG 生成的 C++ DLL 引用添加到 C# 项目?