[英]Swift label which gets text from url is changing size when scrolling collectionView
In a CollectionView there are multiple Cells. 在CollectionView中有多个单元格。 All of them have a genreLabel with text from url.
它们都有一个genreLabel,其中包含来自url的文本。 The text is getting displayed, but when I scroll in the CollectionView, some genreLabel are getting smaller, so the text is not completely displayed anymore.
文本正在显示,但是当我在CollectionView中滚动时,某些genreLabel越来越小,因此文本不再完全显示。 The function
setupViews()
is used to create constraints in CollectionViewCell and properties are set in the function configure()
. 函数
setupViews()
用于在CollectionViewCell中创建约束,并在属性configure()
中设置属性。 Also there is a second problem coming with that: when the genreLabel is getting smaller, the discountLabel
is disappearing too. 也有是与未来的第二个问题:当genreLabel越来越小,
discountLabel
正在消失过。
I tried to put the call of setupViews()
in different places. 我试图将
setupViews()
的调用放在不同的位置。 When I put it in the init of CollectionViewCell the genreLabel is so small, it only shows one Letter of the text. 当我将它放在CollectionViewCell的init中时,genreLabel很小,它仅显示文本的一个字母。 When I put it at the end of
configure()
or after cell.configure()
in collectionView(cellForItemAt)
I get the problem I have right now. 当我将它放在
configure()
的末尾或collectionView(cellForItemAt)
cell.configure()
,我遇到了我现在遇到的问题。
The CollectionViewController: CollectionViewController:
class HomeController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, HomeControllerDelegate {
lazy var eventCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 25
layout.minimumLineSpacing = 25
let cv = UICollectionView(frame: CGRect(x: 25, y: 108, width: UIScreen.main.bounds.width - 50, height: UIScreen.main.bounds.height - 108), collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
var homeEvents = [HomeEvent]()
let homeCellId = "HomeCellId"
override func viewDidLoad() {
super.viewDidLoad()
//Setting up the NavigationController...
HomeModel.shared.getEvents {
(response) in
self.homeEvents = response.homeEvents
self.eventCollectionView.reloadData()
}
eventCollectionView.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
imageCache.removeAllObjects()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return homeEvents.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = eventCollectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as? HomeCell
else {
return UICollectionViewCell()
}
let event = homeEvents[indexPath.item]
cell.configure(with: event)
// cell.setupViews()
cell.homeControllerDelegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if view.frame.width < 415.0 {
return CGSize(width: view.frame.width - 50, height: view.frame.width - 60)
}
else if view.frame.width < 828.0 {
return CGSize(width: (view.frame.width / 2) - 50, height: (view.frame.width / 2) - 60)
}
else {
return CGSize(width: (view.frame.width / 3) - 50, height: (view.frame.width / 3) - 60)
}
}
}
The CollectionViewCell: CollectionViewCell:
class HomeCell: UICollectionViewCell {
var themePage = false
var banner = 0
var gift = 0
var teamEvent = 0
var discount = 1
func configure(with homeEvent: HomeEvent) {
homeEvent.image {
(image) in
self.eventImageView.image = image
}
genreLabel.text = homeEvent.genre
if homeEvent.teamEvent == "0" {
teamEventLabel.isHidden = true
}
else {
teamEvent = 1
teamEventLabel.isHidden = false
}
if homeEvent.gift == "0" {
giftLabel.isHidden = true
}
else {
gift = 1
giftLabel.isHidden = false
}
if homeEvent.discount.prefix(1) == "<" {
let normalText = "Bis zu "
let boldText = "" + homeEvent.discount.suffix(2) + "%"
let normalAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]
let attributedString = NSMutableAttributedString(string: normalText, attributes: normalAttributes)
let boldAttributes = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 24)]
let boldString = NSMutableAttributedString(string: boldText, attributes: boldAttributes)
attributedString.append(boldString)
variableDiscountLabel.attributedText = attributedString
fixDiscountLabel.isHidden = true
discount = 2
}
else {
fixDiscountLabel.text = homeEvent.discount.suffix(2) + "%"
variableDiscountLabel.isHidden = true
}
if homeEvent.bannerText == "" {
bannerView1.isHidden = true
bannerView2.isHidden = true
bannerView3.isHidden = true
bannerView4.isHidden = true
}
else {
if homeEvent.bannerText == "Angebot endet heute!" {
banner = 1
}
else if homeEvent.bannerText == "Überregionales Top-Angebot" {
banner = 2
}
else if homeEvent.bannerText == "Nur noch wenige Tickets" {
banner = 3
}
else if homeEvent.bannerText == "Angebot endet morgen!" {
banner = 4
}
else {
bannerView1.isHidden = true
bannerView2.isHidden = true
bannerView3.isHidden = true
bannerView4.isHidden = true
}
}
eventTitleLabel.text = homeEvent.title
eventSubTitleLabel.text = homeEvent.subtitle
if homeEvent.themePage == "1" {
themePage = true
locationImageView.isHidden = true
eventDatesLabel.isHidden = true
eventLocationLabel.isHidden = true
}
else {
eventDatesLabel.text = homeEvent.dates
eventLocationLabel.text = homeEvent.location
}
setupViews()
}
override func prepareForReuse() {
eventImageView.image = nil
genreLabel.text = nil
teamEventLabel.isHidden = true
giftLabel.isHidden = true
fixDiscountLabel.text = nil
variableDiscountLabel.text = nil
eventTitleLabel.text = nil
eventSubTitleLabel.text = nil
eventDatesLabel.text = nil
eventLocationLabel.text = nil
}
weak var homeControllerDelegate: HomeControllerDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
// setupViews() //genreLabel just displaying one Letter
}
func setupViews() {
addSubview(genreLabel)
if discount == 1 {
addSubview(fixDiscountLabel)
}
else {
addSubview(variableDiscountLabel)
}
//setting Subviews for other labels, views ...
_ = genreLabel.anchor(topAnchor, left: leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: genreLabel.intrinsicContentSize.width, heightConstant: 30)
if discount == 1 {
_ = fixDiscountLabel.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: imageHeight - 32, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 64, heightConstant: 64)
_ = eventTitleLabel.anchor(eventImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: fixDiscountLabel.leftAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: (UIScreen.main.bounds.width - 60 - imageHeight) / 4 - 6)
}
else {
_ = variableDiscountLabel.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: imageHeight - 32, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 64, heightConstant: 64)
_ = eventTitleLabel.anchor(eventImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: variableDiscountLabel.leftAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: (UIScreen.main.bounds.width - 60 - imageHeight) / 4 - 6)
}
//setting the constraints for other labels, views ...
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I want the genreLable to have the width to fit the text and not to change after scrolling. 我希望genreLable的宽度适合文本,并且滚动后不改变。 Also the discountLabel should not disappear after scrolling.
滚动后,DiscountLabel也不应消失。
Try to add the labels into the contentView of the CollectionViewCell, not the view itself. 尝试将标签添加到CollectionViewCell的contentView中,而不是视图本身。
Instead of: 代替:
addSubview(label)
do 做
contentView.addSubview(label)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.