如何使用Color填充CGRect创建的Rectangle

时间:2017-01-09 04:30:38

标签: swift2 swift3

下面的代码不会为Rect1,2,3

绘制CGRect
let context = UIGraphicsGetCurrentContext()

let Rect1 = CGRect(x:30, y:200, width:300, height: 50)
let Rect2 = CGRect(x:30, y:260, width:300, height: 50)
let Rect3 = CGRect(x:30, y:320, width:300, height: 50)

CGContextAddRect(context,Rect1)
CGContextAddRect(context,Rect2)
CGContextAddRect(context,Rect3)

CGContextsetFillColorWithColor(context, UIColor.blueColor().CGColor)

CGContextFillRect(Context,Rect1)
CGContextFillRect(Context,Rect2)
CGContextFillRect(Context,Rect3)

let str1: NSString =" Hello"
let str2: NSString =" How are u?"
let str3: NSString =" See u"

let font = UIFont(name: "Helvetica Neue", size: 18.0)

let textcolor:UIColor = UIColor.whiteColor()

let attr = [NSFontAttributedName:font,
            NSForegroundColorAttributedName: textColor]

str1.drawInREct(Rect1, withAttributes: attr)
str2.drawInREct(Rect2, withAttributes: attr)
str3.drawInREct(Rect3, withAttributes: attr)

....

感谢。

0 个答案:

没有答案