I am trying to draw a string in a UIView.
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) {
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
string.draw(in: rect, withAttributes: [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 24)])
}
}
So there is a view and I overrode UIViews function draw(in:) to write a string in a view. So it writes a string but not it specified rectangle (CGRect(x: 100, y: 300, width: 50, height: 10)) . It just draw it in CGrect(x:0,y:0,width: frame.width, height: frame.height). So it changes rect by itself during runtime.
Here it is:
1) programm begins to call anotheR.draw(CGRect(x: 100, y: 300, width: 50, height: 10)) method.

2) CGRect somehow changes to CGRect(x:0,y:0,width: frame.width, height: frame.height)
Please maybe someone knows whats happen? It exposed all code I had wrote. Thank you!
drawRectby yourself. It's called by the framework when the view is marked to be redrawn. - vadian