简体   繁体   English

迅捷4将图像文字添加到字符串

[英]swift 4 add a image literal to a string

I want to be able to add an image to a string with some text. 我希望能够将图像添加到带有一些文本的字符串中。 There was an example video on headerViews and he included the data that had the images embedded in the string. headerViews上有一个示例视频,他包含了将图像嵌入字符串中的数据。

I am not sure how he did it. 我不确定他是怎么做到的。

Here is the data 这是数据

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 is a simple data structure: 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
}

I tried using #imageLiteral(resourceName) with some text afterwards like 我后来尝试将#imageLiteral(resourceName)与一些文本一起使用

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

which is not syntactically correct. 这在语法上是不正确的。

How do I add an image before some text in a string? 如何在字符串中的某些文本之前添加图像?

Those are not image literals; 这些不是图像文字; they are ordinary characters (if you regard emoji as ordinary). 它们是普通字符(如果您将表情符号视为普通字符)。 Swift strings are unicode strings, so you are allowed to type an emoji character directly into a string literal. Swift字符串是unicode字符串,因此您可以直接在字符串文字中键入表情符号字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM