Swift : 如何将图像设置为适合标签大小的标签?
Swift : How to set image to label that fits as per label size?
我有以编程方式生成的 UICollectionViewCell。单元格上放置了 标签。
现在,我想将图像设置为单元格上的标签,但该图像应根据单元格的大小调整大小。
下面是使用的代码,但没有用:
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。但是这里出现了另一个问题,例如文本对齐,图像未设置..
. . . //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
. . .
你可以尝试在设置框架后设置 UIImageView 的内容模式吗?可以按如下方式进行
objImage.contentMode = UIViewContentMode.ScaleToFill
您可以根据需要使用 ScaleToFill
或 ScaleAspectFill
。您可以找到有关内容模式的更多信息 here
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]
}
将您的按钮图像视图内容模式设置为 ScaleAspectFit
,同时将您的按钮设置为 属性 setImage
而不是 setBackgroundImage
?
yourbutton.imageView?.contentMode =.ScaleAspectFit
yourbutton.contentHorizontalAlignment = .Fill
yourbutton.contentVerticalAlignment = .Fill
yourbutton.setImage(UIImage(named: stretchImage)!, forState: .Normal)
有关其他帮助,请参阅 this
我有以编程方式生成的 UICollectionViewCell。单元格上放置了 标签。
现在,我想将图像设置为单元格上的标签,但该图像应根据单元格的大小调整大小。
下面是使用的代码,但没有用:
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。但是这里出现了另一个问题,例如文本对齐,图像未设置..
. . . //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
. . .
你可以尝试在设置框架后设置 UIImageView 的内容模式吗?可以按如下方式进行
objImage.contentMode = UIViewContentMode.ScaleToFill
您可以根据需要使用 ScaleToFill
或 ScaleAspectFill
。您可以找到有关内容模式的更多信息 here
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]
}
将您的按钮图像视图内容模式设置为 ScaleAspectFit
,同时将您的按钮设置为 属性 setImage
而不是 setBackgroundImage
?
yourbutton.imageView?.contentMode =.ScaleAspectFit
yourbutton.contentHorizontalAlignment = .Fill
yourbutton.contentVerticalAlignment = .Fill
yourbutton.setImage(UIImage(named: stretchImage)!, forState: .Normal)
有关其他帮助,请参阅 this