【问题标题】:Change icon position in swift 3在swift 3中更改图标位置
【发布时间】:2017-11-29 21:28:38
【问题描述】:

我有以下代码,它在 uilabel 的左侧显示一个图标,我的问题是如何将图标放在 uilabel 的右侧:

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "ic_person")
attachment.bounds = CGRect(x: 0, y: -3, width: 15, height: 15)
let attachmentStr = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: "")
myString.append(attachmentStr)
let myString1 = NSMutableAttributedString(string: item_empresa[indexPath.item].nombre_emp!)
myString.append(myString1)
cell.lbl_nombreEmpresa.attributedText = myString

提前致谢

【问题讨论】:

    标签: ios swift nsattributedstring


    【解决方案1】:

    您可以通过在附加文本本身后附加附件来将图像放在文本之后

    let attachment = NSTextAttachment()
    attachment.image = UIImage(named: "ic_person")
    attachment.bounds = CGRect(x: 0, y: -3, width: 15, height: 15)
    
    let attributedString = NSMutableAttributedString(string: item_empresa[indexPath.item].nombre_emp ?? "")
    
    attributedString.append(NSAttributedString(attachment: attachment))
    cell.lbl_nombreEmpresa.attributedText = attributedString
    

    【讨论】:

    • 我的朋友我怎样才能改变我的图像的颜色?
    • @DiegoIsraelMoreno 我在github 上为您创建了这个 UIImage 扩展,只需添加文件并像这样使用它image.with(color: .white)
    • 我有这个调用'with(color:)'的警告结果是未使用的
    • 你是不是先从 GitHub 添加文件
    • 是的,名称为 UIImage.swift
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2021-11-05
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多