2017-04-07 38 views
2

最後一行下面的解決方案正常工作的結束位置僅適合在一行確定的UILabel

 cell.accessoryTitleLabel.text = data.title 
     cell.accessoryTitleLabel.sizeToFit() 
     cell.discountIcon.frame.origin.x = cell.accessoryTitleLabel.frame.maxX + 7 
     cell.discountIcon.hidden = discount == 0 

但我需要把折扣圖標在最後一行的末尾名: enter image description here

+1

選中此http://stackoverflow.com/questions/19318421/how-to-embed-small-icon-in-uilabel/42721312#42721312 –

+0

感謝大家。我可以更改附件圖像的alpha參數嗎? –

回答

2

,最好的辦法是通過NSTextAttachment調整圖像按規定,這樣,你就不必計算任何間距和寬度直接標籤上插入你的形象。


斯威夫特3溶液

var img_attachment = NSTextAttachment() 
img_attachment.image = UIImage(named: "name_of_image") 
img_attachment.bounds = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(ImgWidth), height:CGFloat(ImgHeight)) // you can specify the size and bounds of discount image 
var attributedString = NSAttributedString(attachment: img_attachment) 
var lblString = NSMutableAttributedString(string: "your text here") 
lblString.append(attributedString) 
cell.accessoryTitleLabel.attributedText = lblString 

+0

謝謝,它工作。我可以更改附件圖像的alpha參數嗎? –