let string=nsattributedstring(
字符串:“0”,
属性:attributedit)
let size=string.size()
打印(大小)
设traitCollection=uiTraitCollection(显示比例:1.0)
let renderFormat=uiGraphicsImagerenderFormat(用于:traitCollection)
let renderer = UIGraphicsImageRenderer(bounds: CGRect(origin: .zero, size: size), format: rendererFormat)
let im = renderer.image{ (context) in
UIColor.black.setStroke()
let element = CGFloat(grayShade)/CGFloat(255)
let color = UIColor(red: element, green: element, blue: element, alpha: 1)
print(color)
color.setFill()
context.stroke(renderer.format.bounds)
context.fill(renderer.format.bounds)
let rect = context.format.bounds
print(rect)
string.draw(in: rect)
}
let cg = im.cgImage
return cg!