gpt4 book ai didi

ios - 以编程方式在 UICollectionView 中对齐 UILabel 大小

转载 作者:行者123 更新时间:2023-11-29 00:26:23 25 4
gpt4 key购买 nike

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

最佳答案

您可以采用带约束的自动布局方法或使用框架,下面是代码在 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

关于ios - 以编程方式在 UICollectionView 中对齐 UILabel 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com