我正在尝试在 PDF 文档的整个顶部填充颜色的标题。
无论我将 CGRect 设置为多大以剪辑到文本的边缘,标准行为似乎都是如此。
let attributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
NSAttributedString.Key.backgroundColor : UIColor.red,
NSAttributedString.Key.foregroundColor : UIColor.white
]
let text = "Hello World!"
text.draw(in: CGRect(x: 0, y: 10, width: 2000, height: 2000), withAttributes: attributes)
见附图。
我做了一个狡猾的解决方案,但对它不满意,因为用户可以复制“空格”而不是实际测试。真的不是一个优雅的解决方案:
let attributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
NSAttributedString.Key.backgroundColor : UIColor.red,
NSAttributedString.Key.foregroundColor : UIColor.white
]
let attributesBack = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 32),
NSAttributedString.Key.backgroundColor : UIColor.red,
NSAttributedString.Key.foregroundColor : UIColor.white
]
let line = " "
let text = "Hello World!"
line.draw(in: CGRect(x: 0, y: 0, width: 2000, height: 2000), withAttributes: attributesBack)
text.draw(in: CGRect(x: 0, y: 10, width: 2000, height: 2000), withAttributes: attributes)
Thera 是一种方式吗:要设置 CGRect 文本被绘制到整个页面上,就像 Spacer() 一样?湾。一种绘制实际矩形然后在其上绘制文本的方法?