English 中文(简体)
UTF-8 案文领域未适当开展工作的特性限制
原标题:UTF-8 character limit on TextField not working properly

我认为,案文方面需要有限。 它用固定案文进行罚款。 但是,我需要考虑到特殊的特性和情感,在案文领域达到极限时,我删除了一种特性,并多次提出,例如¢(2个字塔)或欧元(3个字塔),其行为方式就好像根本不存在性限制,你可以提出任何意见。

TextField("Username", text: $ame)
    .onChange(of: name, perform: { value in
        totalBytes = value.utf8.count
        // Only mess with the value if it is too big
        if totalBytes > 14 {
            let firstNBytes = Data(name.utf8.prefix(14))
            if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) {
                // Set the message back to the last place where it was the right size
                name = maxBytesString
            } else {
                print("not a valid UTF-8 sequence")
            }
        }
    })

“UTF-8”警告说,“UTF-8”警告不有效,但在这种近似情况下,文本现场从未得到纠正。 任何帮助? BTW, “名称”是一种“国家变量”,如果是的话,则以<代码>onAppear方法成功启动。

问题回答

议题是let firstNBytes = 数据(名称:utf8.prefix(14)。 正如你所看到的那样,某些特性超过1个。 当你简单地拿走第14条时,你就能够轻松地把一种特性切割一半。 因此,从拖拉机中创建<编码>String的企图可能失败。

从14岁开始,在你拥有有效数据之前,你需要一种办法,以逐个地对数据进行分类。 归根结底,你可能只得到13、12、11或更少的tes,但至少有不足14的str。

此处为<代码>String的延伸,该编码将根据按特限供应的压缩舱位:

extension String {
    func truncated(to bytes: Int, encoding: String.Encoding = .utf8) -> String? {
        if let data = self.data(using: encoding) {
            var tmpData = data.prefix(bytes)
            repeat {
                if let res = String(data: tmpData, encoding: encoding) {
                    return res
                } else {
                    tmpData = tmpData.dropLast(1)
                }
            } while !tmpData.isEmpty
        }

        return nil // Should never be reached with UTF-8 encoding
    }
}

你们的法典成为以下内容:

TextField("Username", text: $name)
    .onChange(of: name, perform: { value in
        totalBytes = value.utf8.count
        // Only mess with the value if it is too big
        if totalBytes > 14 {
            if let newName = value.truncated(to: 14) {
                name = newName
            }
        }
    })




相关问题
Message charset

How do I get the charset from javax.mail.Message object?

Adding "charset" to all ASP.NET MVC HTTP responses

Is there an easy way to specify all "normal" views is an ASP.NET MVC app are to have charset=utf-8 appended to the Content-Type? View() lacks an override that allows you to specify the Content-Type, ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Special characters are not supported

I am having problem to display the special characters like ’, é in Firefox and IE. But these characters are supported for the local server. I have used the following <!DOCTYPE html PUBLIC "-...

找到一份所有编码的一览表。

我正在撰写一篇文章,试图将 by铁丝带入第2.6条的许多不同的编码。 是否有办法获得一份清单,列出我可以重复的可用编码?

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

Flash coding problem

I got some information in my Flash application from a php script. Flash displayed it as: Radioart=Mia Frejman - Ett hjärta - Ett Hjärta The string contain some Swedish symbols. How I can output ...

热门标签