9

I have got some strange problem while using NSMutableAttributedString for setting text part on UILabel. It shows some bizarre symbol for specific emojis. Here's the code i use and the screenshot of the problem.

guard var _comment = comment.comment ,let _username = comment.userName else { return }
var username = NSMutableAttributedString.init(string: _username)
var commentText = NSMutableAttributedString.init(string: _comment)
var commentTotal = NSMutableAttributedString.init()
commentTotal.append(username)
commentTotal.append(commentText)
self.userNameLabel.attributedText = commentTotal

Screenshot :

enter image description here

But if i directly put the string without using NSMutableAttributedString like this:

self.userName.text = _comment

The output of this shows the correct emoji without problem .What would be the problem here? Anyone with a suggestion? enter image description here

This is the code for setting fonts :

if let font = UIFont.init(name: "Montserrat-Bold", size: self.userNameLabel.font.pointSize){
        username.addAttribute(NSFontAttributeName, value: font, range: NSRange.init(location: 0, length: _username.count))
        username.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0), range: NSRange.init(location: 0, length: _username.count))
    }



if let font = UIFont.init(name: "Montserrat-Medium", size: self.userNameLabel.font.pointSize-1){
                    commentText.addAttribute(NSFontAttributeName, value: font, range: NSRange.init(location: 0, length: commentString.count))
                    commentText.addAttribute(NSForegroundColorAttributeName, value: UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0), range: NSRange.init(location: 0, length: commentString.count))
                }

Storyboard Image: enter image description here

6
  • That's probably because you are setting the (not a) wrong font family to the attributed string or UILabel.
    – El Tomato
    Jun 20, 2018 at 19:02
  • What are the strings _username and _comment? A self-contained reproducible example would be helpful.
    – Martin R
    Jun 20, 2018 at 19:02
  • @MartinR _comment : "💃👍👍" and username = "canhamzacan"
    – umutg4d
    Jun 20, 2018 at 19:09
  • @ElTomato I updated the question for more information about fonts. I use different font family and font size for different part of the string. Is it because of that?
    – umutg4d
    Jun 20, 2018 at 19:25
  • @rmaddy Thanks a lot for help. i couldnt search it with right words.
    – umutg4d
    Jun 20, 2018 at 20:28

2 Answers 2

25

Your problem is with your NSRange calculations when setting the attributes. NS[Mutable]AttributeString needs the NSRange to be based on NSString ranges, not String ranges.

So code like:

NSRange.init(location: 0, length: commentString.count)

Needs to be written as either:

NSRange(location: 0, length: (commentString as NSString).length)

or:

NSRange(location: 0, length: commentString.utf16.count)

The following demonstrates the issue with commentString.count:

let comment = "💃👍👍"
print(comment.count) // 3
print((comment as NSString).length) // 6
print(comment.utf16.count) // 6

This is why your code seems to be splitting the middle character in half. You are passing in half (in this case) the needed length.

1
  • Bravo. It helps me a lot!
    – HamasN
    May 25, 2022 at 9:47
3

Proper way to do this in Swift 4 is using indexes on String:

NSRange(location: 0, length: commentString.endIndex.encodedOffset)
1
  • Interesting. String.Index encodedOffset is based on the UTF-16 encoded string. So string.endIndex.encodedOffset will be the same as string.utf16.count.
    – rmaddy
    Jun 20, 2018 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.