iOS富文本 图片上下居中

2023-03-28  本文已影响0人  秋叶红90
        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"))
上一篇 下一篇

猜你喜欢

热点阅读