gpt4 book ai didi

ios - swift : How to set image to label that fits as per label size?

转载 作者:行者123 更新时间:2023-11-28 11:04:33 25 4
gpt4 key购买 nike

我有以编程方式生成的 UICollectionViewCell。单元格上有标签。

现在,我想将图像设置为单元格上的标签,但该图像应根据单元格的大小调整大小。

Screenshot 1

下面是使用的代码,但没有用:

 let objImage : UIImage = UIImage(named: "OK")!
let objCGSize = cell.lblNumber?.frame.size

UIGraphicsBeginImageContext(objCGSize!)
objImage.drawInRect(CGRectMake(0, 0, (cell.lblNumber?.frame.size.width)!, (cell.lblNumber?.frame.size.height)!))

let objNewImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

cell.lblNumber!.text = ""
cell.lblNumber!.backgroundColor = UIColor(patternImage: objNewImage)

任何其他修复?

我尝试按照某人的建议使用 UIButton 而不是 UILabel。但是这里出现了另一个问题,例如文本对齐,图像未设置..

Screenshot 2

. . . //Code for UIButton instead of UILabel

cell.btnNumber.setTitle("", forState: .Normal)
//cell.btnNumber.setImage(UIImage(named: "OK"), forState: .Normal)
cell.btnNumber.imageView?.image = UIImage(named: "OK")
cell.btnNumber.imageView?.contentMode = UIViewContentMode.ScaleAspectFit

. . .

最佳答案

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("hobbiesCell", forIndexPath: indexPath) as! HobbiesTagCell
self.configureCell(cell, forIndexPath: indexPath)
return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
self.configureCell(self.sizingCell!, forIndexPath: indexPath)
return self.sizingCell!.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
}
func configureCell(cell: HobbiesTagCell, forIndexPath indexPath: NSIndexPath) {
let tag = tags[indexPath.row]
cell.hobbiesTagName.text = yourArrname[indexPath.item]
}

关于ios - swift : How to set image to label that fits as per label size?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38913443/

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