美文网首页
iOS富文本 图片上下居中

iOS富文本 图片上下居中

作者: 秋叶红90 | 来源:发表于2023-03-28 16:55 被阅读0次
        self.textLabel.font = .systemFont(ofSize: 50)
        // 调好比例
        let rate = self.textLabel.font!.pointSize / (32)
        
        
        let image = UIImage(named: "xxxx")!
        
        print("self.textLabel.font!.capHeight == \(self.textLabel.font!.capHeight) \(self.textLabel.font!.pointSize) == \(image.size)")
        let size: CGSize = .init(width: image.size.width * rate, height: image.size.height * rate)
        let attachment = NSTextAttachment(image: image)
        //  capHeight 来计算 y 坐标偏移量 这样可以让图片上下居中
        attachment.bounds = CGRect.init(x: 0, y: (self.textLabel.font!.capHeight - size.height) / 2, width: size.width, height: size.height)
        let first = NSMutableAttributedString(attachment: attachment)
        
        first.append(NSAttributedString.init(string: "10000"))

相关文章

网友评论

      本文标题:iOS富文本 图片上下居中

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