Reeve
Reeve

Reputation: 11

How to fit custom UIBezierPath into NSTextContainer

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.sstatic.net/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
}

Upvotes: -1

Views: 198

Answers (1)

Alex
Alex

Reputation: 8991

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.

Upvotes: 1

Related Questions