【问题标题】:Determining the position of the end of the last line in UILabel [duplicate]确定 UILabel 中最后一行结尾的位置 [重复]
【发布时间】:2017-09-02 14:02:26
【问题描述】:

以下解决方案仅适用于适合一行的名称

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

但我需要在最后一行的末尾放置一个折扣图标:

【问题讨论】:

标签: ios swift user-interface labels


【解决方案1】:

最好的方法是通过NSTextAttachment直接在标签上插入图片根据要求调整图片大小,这样您就不必计算任何间距和宽度。


Swift 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

【讨论】:

  • 谢谢,它成功了。我可以更改附件图像的 alpha 参数吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
相关资源
最近更新 更多