【问题标题】:Align UILabel Size Inside UICollectionView Programatically以编程方式在 UICollectionView 内对齐 UILabel 大小
【发布时间】:2017-03-20 09:37:44
【问题描述】:

我在我的应用程序中使用 UICollectionView。问题是我在 UICollectionView 中有多个 UILabel。 UILabel 的宽度大于collectionview 的宽度。所以它会重叠并且看起来不太好。如何以编程方式设置collectionview标签的宽度

【问题讨论】:

  • UILabelUICollectionView 中,你的意思是在UICollectionViewCell 中?你能给出你的问题的截图,也许还有一些代码?你使用 NSLayoutConstraints 吗?
  • 显示你尝试过的代码

标签: ios swift3 uicollectionview


【解决方案1】:

您可以采用带有约束或使用框架的自动布局方法,这是代码在 Objective-C 中的外观:

// using auto layout
self.label.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                 attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.contentView
                                                 attribute:NSLayoutAttributeWidth
                                                multiplier:0.8
                                                  constant:0]];


//using frames
CGFloat labelWidth = CGRectGetWidth(self.contentView.bounds)*0.8;
self.label.frame = CGRectMake(DESIRED_X_POSSITION, DESIRED_Y_POSSITION, labelWidth, DESIRED_HEIGHT);

// you can set your labels' number of lines to 0 so your text goes to the next line
self.label.numberOfLines = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2017-08-22
    • 2020-10-04
    相关资源
    最近更新 更多