【发布时间】:2017-07-24 21:30:35
【问题描述】:
假设我有一些文本:“梯形中的一些属性文本。”
我有 NSAttributedString 扩展,它返回带有属性文本的 UIImage:
extension NSAttributedString {
func asImage() -> UIImage? {
defer {
UIGraphicsEndImageContext()
}
let size = boundingRect(with: CGSize.zero, options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], context: nil).size
UIGraphicsBeginImageContext(size)
draw(at: CGPoint.zero)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
但是由于使用了boundingRect,这个函数在一行中返回了我的文本:
------------------------------------
|Some attributed text in trapezoid.|
------------------------------------
如果我要使用自定义矩形来绘制文本,那将无济于事...
UIGraphicsBeginImageContext(CGRect(x: 0, y: 0, width: 100, height: 30))
draw(at: CGPoint.zero)
...因为文本将在矩形中:
--------------
|Some attribu|
|ted text in |
|trapezoid. |
--------------
我需要的是在具有已知角位置的梯形中(或在具有已知半径的圆中)绘制文本。所以每一行新的文本都应该以一个小的偏移量开始,见例子:
所以我想看到类似的东西:
---------------
\Some attribut/
\ed text in /
\trapezoid/
---------
我怎样才能达到这个结果?
【问题讨论】:
-
这篇文章提供了一个好的开始。 developer.apple.com/library/content/documentation/…
-
对于基于圆形的形状,我认为这是您的解决方案:How to fit text in a circle in UILabel 并将其用于梯形 不应该(尚未测试)因为它太远了意味着将排除区域定义为 2 个直角三角形
-
添加到 BallPointBen,也许有一些想法:stackoverflow.com/questions/26632474/…
标签: swift uikit nsattributedstring uigraphicscontext