Using NSMutableParagraphStyle cause issue with emojis

后端 未结 1 712
慢半拍i
慢半拍i 2021-01-21 11:27

In my app I want to change the line height, I am using this string extension :

extension String {
    func addLineHeightWith(alignement: NSTextAlignment) -> N         


        
相关标签:
1条回答
  • 2021-01-21 11:56

    The problem is with your use of NSRange(location: 0, length: self.count).

    self.count is the proper number of characters in the Swift String. But the NSAttributedString is based on NSString and its use of UTF-16 encoded characters. You end up applying the style to only about half of the actual string. In fact, it splits one of the characters in half.

    The easy fix is to get the length of the string as an NSString.

    Replace:

    NSRange(location: 0, length: self.count)
    

    with:

    NSRange(location: 0, length: (self as NSString).length))
    
    0 讨论(0)
提交回复
热议问题