我有一个UIBezierPath
的实例,我想将笔画的颜色改为黑色以外的其他颜色。有谁知道如何在Swift中做到这一点?如何在Swift中更改UIBezierPath的颜色?
11
A
回答
27
随着Swift 3,UIColor
有一个setStroke()
方法。 setStroke()
有如下声明:
func setStroke()
设置后续行程操作来接收机表示颜色的颜色。
因此,你可以使用setStroke()
这样的:
strokeColor.setStroke() // where strokeColor is a `UIColor` instance
下面的游乐场代码演示了如何使用setStroke()
一起UIBezierPath
为了画一个圆,一个绿色的填充色和光一个子类UIView
内部灰色笔划颜色:
import UIKit
import PlaygroundSupport
class MyView: UIView {
override func draw(_ rect: CGRect) {
// UIBezierPath
let newRect = CGRect(
x: bounds.minX + ((bounds.width - 79) * 0.5 + 0.5).rounded(.down),
y: bounds.minY + ((bounds.height - 79) * 0.5 + 0.5).rounded(.down),
width: 79,
height: 79
)
let ovalPath = UIBezierPath(ovalIn: newRect)
// Fill
UIColor.green.setFill()
ovalPath.fill()
// Stroke
UIColor.lightGray.setStroke()
ovalPath.lineWidth = 5
ovalPath.stroke()
}
}
let myView = MyView(frame: CGRect(x: 0, y: 0, width: 200, height: 300))
PlaygroundPage.current.liveView = myView
1
假设你想用红色来代替笔划圆角矩形; 这是你怎么做的雨燕3:
// Drawing the border of the rounded rectangle:
let redColor = UIColor.red
redColor.setStroke() // Stroke subsequent views with a red color
let roundedRectagle = CGRect(x: 0,y: 0, width: 90,height: 20)
let rectangleBorderPath = UIBezierPath(roundedRect: roundedRectangle,cornerRadius: 5)
roundedRectangle.borderWidth = 1
roundedRectangle.stroke() // Apply the red color stroke on this view
上面代码的第二行和最后一行是在回答你的question.I希望这个答案是有帮助的重要。
相关问题
- 1. 在Swift中更改UIBezierPath的颜色
- 2. 如何在Swift中添加视图后更改UIBezierPath的颜色?
- 3. 如何更改UIBezierPath背景颜色?
- 4. 如何在swift中更改标签栏的色调颜色?
- 5. 更改accessoryType的颜色Swift
- 6. 如何在UIBezierPath中填充颜色?
- 7. 在Swift TextView中更改打印颜色
- 8. Swift UITableviewCell颜色更改
- 9. 如何在swift中更改tableView节中的文本颜色
- 10. 更改Swift中UITabBarItem的背景颜色
- 11. 如何在swift中更改子视图的背景颜色?
- 12. UIBezierPath多行颜色
- 13. 如何在swift中更改搜索栏文字颜色
- 14. 如何在swift中选择时更改google maps标记颜色?
- 15. 如何在Swift上使用drawRect更改三角形的颜色
- 16. 如何更改标签中特定单词的颜色 - Swift 3
- 17. 如何更改tableview swift中文本的颜色?
- 18. 如何更改Swift 3中UISearchBar“取消”按钮的颜色?
- 19. UIBezierPath不同的颜色
- 20. 如何更改背景视图颜色偏爱在ios swift
- 21. 如何更改光标颜色而不更改文字颜色?
- 22. UITableView如何更改颜色?
- 23. 如何更改ttk.Separator颜色
- 24. 如何更改GroupStyle颜色?
- 25. 如何更改颜色
- 26. 如何更改createCircularReveal()颜色?
- 27. UNITY - 如何更改颜色?
- 28. 如何更改CircularFillableLoaders颜色?
- 29. 如何更改ActionBar颜色
- 30. 如何更改layer.border.top颜色?
POB的答案在这里很有帮助,但重要的是要明白Bézier路径本身没有颜色。这是一条曲线的描述。只有笔有颜色,POB的答案全是关于设置笔的颜色。 – 2014-08-27 17:38:46