这里我试图通过调用它的draw(使用rect:options:context :)函数并将选项参数设置为NSStringDrawingOptions.usesLineFragmentOrigin来绘制NSAttributeString。实际上,是的,NSAttributeString绘制它的字符串,但是如果在draw中更改rect参数(使用rect:options:context :) fucntion,则更小的框架MSAttributeString只会削减它的部分就可以了。但是根据{{3}}
中的.usesLineFragmentOrigin说明如果在options中指定了usesLineFragmentOrigin,它将包装该字符串 文本根据需要使其适合。如果字符串太大而不适合 完全在矩形内部,该方法缩放字体或调整 字母间距使字符串适合给定的边界。 这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let anotheR: anotherView = anotherView(frame: view.bounds)
view.backgroundColor = UIColor.yellow
view.addSubview(anotheR)
anotheR.backgroundColor = UIColor.lightGray
anotheR.draw(CGRect(x: 100, y: 300, width: 50, height: 10))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class anotherView: UIView {
override func draw(_ rect: CGRect) {
frame = CGRect(x: 0, y: 0, width: 500, height: 500)
let suprect = rect
var string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s." as NSString
let attributedString = NSMutableAttributedString(string: string as String)
layer.borderColor = UIColor.red.cgColor
layer.borderWidth = 2
let context = NSStringDrawingContext()
context.minimumScaleFactor = 0.1
attributedString.draw(with: CGRect(x: 20, y: 100, width: 80, height: 300), options: NSStringDrawingOptions.usesLineFragmentOrigin , context: context)
}
}
但是,不,似乎没有做到这一点。也许某个地方我错了?也许它应该以其他方式工作?如果有人知道,请给我一个线索。谢谢!