-1

我正在使用此链接中的方法:How to fit text in a circle in UILabel

while (![circle containsPoint:p])

当我使用原始方法时,[UIBezierPath bezierPathWithOvalInRect:r];效果很好。但是当我绘制自己的 UIBezierPath 并替换它时,它不起作用。while 循环永远持续下去。下面是 (A) 自定义 UIBezierPath 绘图和 (B) 默认 UIBezierPath 椭圆的屏幕截图:http: //i.imgur.com/n74BHjX.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
}
4

1 回答 1

1

如果我正确理解了您的形状,您正在尝试水平扫描以检测重叠,但您使用的路径在平面 y=0 上不存在。您需要更改 的yr.origin或使用到达 y=0 的路径。

于 2015-04-02T13:10:52.557 回答