iOS Swift - CGSizeFromString抛出CGSizeZero [英] iOS Swift - CGSizeFromString throws CGSizeZero
本文介绍了iOS Swift - CGSizeFromString抛出CGSizeZero的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个函数,用于设置从字符串长度的大小。
这样的东西 - Swift iOS - 标签集合视图。
categories中的第一个项目是Age
func collectionView(collectionView:UICollectionView ,
layout collectionViewLayout:UICollectionViewLayout,
sizeForItemAtIndexPath indexPath:NSIndexPath) - > CGSize {
var cellWidth:CGSize = CGSizeFromString(self.categoriesItems [indexPath.row] .name);
return cellWidth
}
解决方案>
要确定文本的宽度,请使用以下代码:
let text = self.categoriesItems [indexPath.row]。 name
let rect = text.boundingRectWithSize(constraintSize,options:NSStringDrawingOptions.UsesFontLeading,attributes:[NSFontAttributeName:UIFont.systemFontOfSize(17)],context:nil)
return rect.size.width
其中 constraintSize
约束您的文本
I have this function which is used to set size from String length. Something like that - Swift iOS - Tag collection view.
First item in categoriesItems is "Age"
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var cellWidth : CGSize = CGSizeFromString(self.categoriesItems[indexPath.row].name);
return cellWidth
}
解决方案
To determine the width of text use the following code
let text = self.categoriesItems[indexPath.row].name
let rect = text.boundingRectWithSize(constraintSize, options: NSStringDrawingOptions.UsesFontLeading, attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17)], context: nil)
return rect.size.width
where constraintSize
is some arbitrary size in which you wish to constraint your text
这篇关于iOS Swift - CGSizeFromString抛出CGSizeZero的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文