-1

I'm using the method in this link: How to fit text in a circle in UILabel

while (![circle containsPoint:p])

When I use the original method, [UIBezierPath bezierPathWithOvalInRect:r]; it works well. But when I draw my own UIBezierPath and substitute it, it doesn't work. The while loop goes on forever. Below is the screenshot of (A) custom UIBezierPath drawing and (B) default UIBezierPath oval: https://i.stack.imgur.com/2xxOs.png

func drawPath() -> UIBezierPath 
{
    var path = UIBezierPath()
    path.moveToPoint(CGPointMake(126,488))
    path.addCurveToPoint(CGPointMake(196,471), controlPoint1: CGPointMake(156,488), controlPoint2: CGPointMake(175,471))
    path.addCurveToPoint(CGPointMake(266,488), controlPoint1: CGPointMake(216,471), controlPoint2: CGPointMake(235,488))
    path.addCurveToPoint(CGPointMake(362,382), controlPoint1: CGPointMake(296,488), controlPoint2: CGPointMake(338,440))
    path.addCurveToPoint(CGPointMake(304,290), controlPoint1: CGPointMake(327,365), controlPoint2: CGPointMake(304,331))
    path.addCurveToPoint(CGPointMake(349,206), controlPoint1: CGPointMake(304,255), controlPoint2: CGPointMake(322,224))
    path.addCurveToPoint(CGPointMake(265,164), controlPoint1: CGPointMake(327,178), controlPoint2: CGPointMake(295,164))
    path.addCurveToPoint(CGPointMake(196,182), controlPoint1: CGPointMake(219,164), controlPoint2: CGPointMake(218,182))
    path.addCurveToPoint(CGPointMake(126,164), controlPoint1: CGPointMake(173,182), controlPoint2: CGPointMake(172,164))
    path.addCurveToPoint(CGPointMake(12,298), controlPoint1: CGPointMake(73,164), controlPoint2: CGPointMake(12,210))
    path.addCurveToPoint(CGPointMake(126,488), controlPoint1: CGPointMake(12,387), controlPoint2: CGPointMake(81,488))

    return path
}

override func lineFragmentRectForProposedRect(proposedRect: CGRect, atIndex characterIndex: Int, writingDirection baseWritingDirection: NSWritingDirection, remainingRect: UnsafeMutablePointer<CGRect>) -> CGRect 
{
    var result = super.lineFragmentRectForProposedRect(proposedRect, atIndex:characterIndex, writingDirection:baseWritingDirection, remainingRect:remainingRect)
    let r = CGRectMake(0,0,self.size.width,self.size.height)
    let circle = drawPath() //UIBezierPath(ovalInRect: r)
    println("this is self created irregular shape: \(path)")
    println("this is the default working OVALLLL: \(UIBezierPath(ovalInRect: r))")

    while !circle.containsPoint(result.origin) {
        result.origin.x += 0.1
        println(result.origin.x)
    }
     while !circle.containsPoint(CGPointMake(result.maxX, result.origin.y)) {
        result.size.width -= 0.1
    }
    return result
}
Community
  • 1
  • 1
Reeve
  • 11
  • 3

1 Answers1

1

If I understand your shape correctly, you're trying to scan horizontally to detect overlap but the path you're using does not exist on the plane y=0. You need to change the y value of r.origin or use a path that reaches y=0.

Alex
  • 8,801
  • 5
  • 27
  • 31
  • Thanks for the prompt reply! If I'd like to change the r.origin, what should it be equal to? – Reeve Apr 02 '15 at 13:30
  • I think you're right. When I println the whileloop. The default oval bezierpath does end at y = 0, but how do I solve that =\ – Reeve Apr 02 '15 at 13:40
  • Scan horizontally until you reach the bounds of the rect, then increase the y position and start scanning horizontally again. Repeat until you get a hit. – Alex Apr 02 '15 at 13:51
  • Could you please post a sample code =P As my brain kinda freeze now and don't know what to do. Thanks a lot! – Reeve Apr 02 '15 at 14:01
  • Hi @Reeve, I think you've got enough information to write the code now that you know the solution. – Alex Apr 03 '15 at 17:20