美文网首页
Swift含有emoji的富文本显示遇到的问题

Swift含有emoji的富文本显示遇到的问题

作者: 深刻的你 | 来源:发表于2023-02-05 14:51 被阅读0次

    原来的代码:

    let attributedContent: NSMutableAttributedString
    NSMutableAttributedString(string: content)
    let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 7 // 大小调整
    attributedContent.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: content.count)) attributedContent.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 14), range: NSRange(location: 0, length: content.count))
    contentLabel.attributedText = attributedContent
    let contentHeight = attributedContent.boundingRect(with: CGSize(width: ltSize.width - 32, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).height
    contentLabel.frame = CGRect(x: 0, y: 0, width: ltSize.width - 32, height: contentHeight)
    

    原来的效果:1、有些表情显示效果不正确,显示方框和?或其它黑色异样表情;2、行数有问题,显示不完整。


    IMG_0515.jpg

    修改后的代码:其实只是修改了Range的length,由content.count改成content.utf16.count。

    let attributedContent: NSMutableAttributedString
    NSMutableAttributedString(string: content)
    let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 7 // 大小调整
    attributedContent.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: content.utf16.count)) attributedContent.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 14), range: NSRange(location: 0, length: content.utf16.count))
    contentLabel.attributedText = attributedContent
    let contentHeight = attributedContent.boundingRect(with: CGSize(width: ltSize.width - 32, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).height
    contentLabel.frame = CGRect(x: 0, y: 0, width: ltSize.width - 32, height: contentHeight)
    

    正确显示的效果:


    IMG_F38FDE0515E8-1.jpeg

    还有一种方式,将Swift的字符串转成OC的NSString

    let attributedContent: NSMutableAttributedString = NSMutableAttributedString(string: content)
    let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 7 // 大小调整
    attributedContent.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: (content as NSString).length))
    attributedContent.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 14), range: NSRange(location: 0, length: (content as NSString).length))
    contentLabel.attributedText = attributedContent
    let contentHeight = attributedContent.boundingRect(with: CGSize(width: ltSize.width - 32, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).height
    contentLabel.frame = CGRect(x: 0, y: 0, width: ltSize.width - 32, height: contentHeight)
    

    分享原因:NSString是由UTF-16单元构成,.length方法返回的是基于UTF-16的长度。Swift的count返回的只是Unicode字符个数,而Swift有专门的utf16.count是对应NSString的.length方法。

    相关文章

      网友评论

          本文标题:Swift含有emoji的富文本显示遇到的问题

          本文链接:https://www.haomeiwen.com/subject/ttcghdtx.html