问题描述
尝试弯曲一些文本。我正在尝试修改Curve text on existing circle,但不能在圆的上下文中使用它,而只是在一条直线上进行,但是老实说,我不确定我是否完全了解该代码在做什么以进行修改
extension CALayer {
func drawCurvedString(text: NSAttributedString,angle: CGFloat,radius: CGFloat) {
var radAngle = angle.radians
let textSize = text.boundingRect(
with: CGSize(width: .max,height: .max),options: [.usesLineFragmentOrigin,.usesFontLeading],context: nil)
.integral
.size
let perimeter: CGFloat = 2 * .pi * radius
let textAngle: CGFloat = textSize.width / perimeter * 2 * .pi
var textRotation: CGFloat = 0
var textDirection: CGFloat = 0
if radAngle > CGFloat(10).radians,radAngle < CGFloat(170).radians {
// bottom string
textRotation = 0.5 * .pi
textDirection = -2 * .pi
radAngle += textAngle / 2
} else {
// top string
textRotation = 1.5 * .pi
textDirection = 2 * .pi
radAngle -= textAngle / 2
}
for c in 0..<text.length {
let letter = text.attributedSubstring(from: NSRange(c..<c+1))
let charSize = letter.boundingRect(
with: CGSize(width: .max,context: nil)
.integral
.size
let letterangle = (charSize.width / perimeter) * textDirection
let x = charSize.width * CGFloat(c)//radius * cos(radAngle + (letterangle / 2))
let y: CGFloat = 0//radius * sin(radAngle + (letterangle / 2))
print("x:\(x),y: \(y)")
let singleChar = drawText(
text: letter,frame: CGRect(
x: (self.frame.size.width / 2) - (charSize.width / 2) + x,y: (self.frame.size.height / 2) - (charSize.height / 2) + y,width: charSize.width,height: charSize.height))
self.addSublayer(singleChar)
singleChar.transform = CATransform3DMakeAffineTransform(CGAffineTransform(rotationAngle: radAngle - textRotation))
radAngle += letterangle
}
}
func drawText(text: NSAttributedString,frame: CGRect) -> CATextLayer {
let textLayer = CATextLayer()
textLayer.frame = frame
textLayer.string = text
textLayer.alignmentMode = CATextLayerAlignmentMode.center
textLayer.contentsScale = UIScreen.main.scale
return textLayer
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)