正如您所看到的,文本只是停止了" ..."。当标签我更大时,我希望尺寸变大,我希望单元尺寸低于标签。我怎样才能做到这一点?希望你理解我的问题..有人请吗?
答案 0 :(得分:1)
您应该使用collectionView:layout:sizeForItemAtIndexPath
中名为UICollectionViewDelegateFlowLayout
的委托方法。
现在,实际的实施将取决于您的具体情况(是本地内容,是否通过网络下载,您拥有多少自定义单元格等),但这里是一个非常简单的副本UICollectionViewController
有效。
它使用MyAwesomeCell
类型的单个自定义单元格,该单元格具有名为myAwesomeLabel
的单个标签出口。
我没有对有关Preferred Width
,Content Compression Resistance Priority
等的故事板进行任何更改。我所做的唯一重要更改是将lines
属性设置为0作为标签,意味着无限量的线条。
PS,它是用Swift 2编写的。
//
// ViewController.swift
// CollectionCell
//
// Created by Stefan Veis Pennerup on 21/09/15.
// Copyright © 2015 Kumuluzz. All rights reserved.
//
import UIKit
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
// MARK: - Constants
struct Constants {
static let ReuseIdentifierMyAwesomeCell = "myAwesomeCell"
}
// MARK: - Models
let myAwesomeCells = [
"Lorem ipsum dolor sit amet.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed massa leo, mollis id tortor at posuere.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque semper vitae mi vel hendrerit. Suspendisse et feugiat mi. Donec quis sollicitudin quam, non porttitor nulla. Phasellus in luctus lorem, sed auctor enim. Suspendisse potenti. Ut maximus pharetra diam, ac laoreet est dignissim eu nullam."
]
var cellSizes: [CGSize]!
// MARK: - Lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
// Gives the size array an initial value since collectionView:layout:sizeForItemAtIndexPath
// is called before collectionView:cellForItemAtIndexPath
cellSizes = myAwesomeCells.map({ _ in return CGSize(width: 200, height: 50) })
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Now that the collection view has appeared, then all the cells have been initialized
// with their appropiate content. The view should then be reloaded with the newly
// calculated sizes as well.
collectionView?.reloadData()
}
// MARK: - UICollectionViewDataSource
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
NSLog("\(self), collectionView:numberOfItemsInSection")
return myAwesomeCells.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
NSLog("\(self), collectionView:cellForItemAtIndexPath")
// Configures the cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Constants.ReuseIdentifierMyAwesomeCell, forIndexPath: indexPath) as! MyAwesomeCell
cell.myAwesomeLabel.text = myAwesomeCells[indexPath.item]
// Calculates the height
cell.setNeedsLayout()
cell.layoutIfNeeded()
cellSizes[indexPath.item] = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
// Returns the new cell
return cell
}
// MARK: - UICollectionViewDelegate
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
NSLog("\(self), collectionView:layout:sizeForItemAtIndexPath")
return cellSizes[indexPath.item]
}
}
答案 1 :(得分:1)
这个答案根本不会改变单元格大小,但它可以解决您的问题。在设置单元格后## Read through the config file and assign items to the global $config variable
function ReadConfig($config_file) {
global $config;
$lines = file($config_file);
foreach ($lines as $line_num => $line) {
$line = rtrim(preg_replace("/#.*/","",$line));
if(preg_match("/\[.*\]/", $line, $parts)) {
$section = $parts[0];
$section = preg_replace("/[\[\]]/","",$section);
} elseif (preg_match("/=/",$line)) {
list($var,$value) = split('=',$line);
$var = preg_replace('/ $/','',$var);
$value = preg_replace('/^ +/','',$value);
$config->{$section}->{$var} = $value; # here
}
}
}
:cellForItemAtIndexPath
尝试添加此内容:let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
这将使字体符合标签大小,文本越多,字体越小。让我知道这对你有用。
答案 2 :(得分:0)
尝试使用相同的文本创建假标签,在其上调用sizetofit,然后使用全文查看标签的实际大小。
然后,一旦获得高度,就可以将其设置为collectionViewLayout itemSize属性。
我相信这种方式所有的集合视图单元格都会更大,但也许它可以成为更好解决方案的起点......