2

Is there a general method to fill an area in Swift.

I have various shape in my UIView, lines, ellipses, curves … etc. Those shapes delimit a certain number of areas. I want to fill one area in a color (uiColorOne) and another area in another color (uiColorTwo).

Is that possible? If YES, how can I do that?

Michel
  • 10,303
  • 17
  • 82
  • 179
  • Read up on `NSBezierPath` ([documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/index.html)) – Arc676 Oct 19 '15 at 09:54
  • I am looking for a general flood-fill solution. I have quickly read about the Bézier Path way before writing my post. But it seems that I have to handle the surrounding curve separately. Hoping I am wrong, I will take a look at the link you suggest. – Michel Oct 19 '15 at 11:07
  • `Is there a general method to fill an area in Swift.`: no, because Swift is a programming language, not a UI framework. What you need is probably in UIKit, which is iOS's UI framework. And you can use it with Swift or Objective-C. – Eric Aya Oct 19 '15 at 12:05

1 Answers1

3
import UIKit

override func drawRect(rect: CGRect)
{
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: 200, y: 200))
    path.addLineToPoint(CGPoint(x: 200, y: 300))
    path.addLineToPoint(CGPoint(x: 300, y: 200))
    path.closePath()
    UIColor.redColor().set()
    path.lineWidth = 1
    path.fill()
}

You will need to override drawRect in order to start making some geometric shaped items in your code (ONLY OVERRIDE AND NEVER CALL IT). The code snippet creates an element based on points (careful not pixels) and fills it with the redColor.You will have to use UIBezierPath most of the times you are playing with elements and so I suggest you have a really good look at it from apple documentation here, since it has some awesome methods that will help you out with your every day struggle : UIBezierPath

Yoichi Tagaya
  • 4,547
  • 2
  • 27
  • 38
Korpel
  • 2,432
  • 20
  • 30
  • My drawRect is already working fine. The problem using UIBezierPath is that I need to prepare a path first and then fill it. And this is not what I want (at least in the best case). I have a bunch of lines and curves, deliminating a number of areas which I then want to flood-fill (if possible without having to worry about the surrounding shape). If I could start coloring one point and then go around recursively (for the neighbouring points), that would be the ideal. – Michel Oct 19 '15 at 13:34
  • you want to programmatically colour each element in your screen? do you mind about the colour (different or same for all?)Could you share some piece of code just to find out what you are trying to make? thanks – Korpel Oct 19 '15 at 13:42
  • At this point I do not really have code to show because not working. I just want to know how I can flood-fill an area. And the area is not predecided. Imagine for example tens of lines and curves all over the view and you will get a number of areas. – Michel Oct 19 '15 at 22:57
  • if all this areas close and those areas are named(path1,path2) etc the you can simply do path1.fill() and path2.fill() .... etc for all paths. you get the point? once a path is closed you can fill it up with any color you want to following the code – Korpel Oct 19 '15 at 23:06
  • Yes I know I can use path.fill IF those areas are named. But they are NOT named. The areas are only the random result of laying out many straight lines and various curves. – Michel Oct 19 '15 at 23:32
  • i see. Perhaps you could fill the whole background first with a colour and once those lines get initialised try to fill with white colour the new view.It's the first time i actually see something like that so i am kinda clueless – Korpel Oct 19 '15 at 23:41
  • In fact I have already done what I need, but that was in objectiv C. So I am now trying to transpose that into Swift, and that is not so easy. I also found this http://stackoverflow.com/questions/30580688/ios-swift-flood-fill-algorithm on SOF, which seems to be close to what I need. But when I try it in my code I get a few error messages, the first one being: Value of type 'UIImage' has no member 'createARGBBitmapContext’. – Michel Oct 24 '15 at 07:52