【问题标题】:swift 4 add a image literal to a stringswift 4 将图像文字添加到字符串
【发布时间】:2019-05-22 19:15:06
【问题描述】:

我希望能够将图像添加到带有一些文本的字符串中。 headerViews 上有一个示例视频,他包含了在字符串中嵌入图像的数据。

我不知道他是怎么做到的。

这是数据

Section(genre: "???? Superhero",
        movies: ["Guardians of the Galaxy", "The Flash", "The Avengers", "The Dark Knight"],
        expanded: false,
        subtitle: "Please select a movie"),
Section(genre: "???? Horror",
        movies: ["The Walking Dead", "Insidious", "Conjuring"],
        expanded: false,
        subtitle: "Please select a movie")

Section是一个简单的数据结构:

struct Section {
    let genre: String!
    let movies: [String]!
    let expanded: Bool!
    let subtitle: String!    

    init(genre: String, movies: [String], expanded: Bool, subtitle: String){
    self.genre = genre
    self.movies = movies
    self.expanded = expanded
    self.subtitle = subtitle
}

之后我尝试使用#imageLiteral(resourceName) 和一些文本,例如

" #imageLiteral(resourceName: "pos") + "a title" "

语法不正确。

如何在字符串中的某些文本之前添加图像?

【问题讨论】:

    标签: swift string image-literals


    【解决方案1】:

    那些不是图像文字;它们是普通字符(如果您将emoji 视为普通字符)。 Swift 字符串是 unicode 字符串,因此您可以将表情符号字符直接输入到字符串文字中。

    【讨论】:

    • 文档的这个页面上有很多例子:docs.swift.org/swift-book/LanguageGuide/…
    • 好吧,您可以将内联图像附加到属性字符串。但这不是你问的。
    • 你是怎么做到的?
    • 当我将它分配给 cell.textLabel?.text 时它会真正起作用吗?
    • @steller 不,但是当您将其分配给 cell.textLabel.attributedText 时它会起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2022-11-19
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多