Subclass UILabel - user-defined method 'drawRect' forces text to disable / not show

I create a simple subclass of "tooltip", which is a rounded rectangle and a small triangle that will be attached to another view.

I subclassed UILabel and redefined 'drawRect' to squeeze the main area of ​​the label and draw a triangle.

"roundRect" represents the rounded rectangular portion that should contain the full text.

All this works fine, except it is difficult for me to get the full text that will be shown within roundRect. It seems that the text simply does not appear for the last line (i.e. where the triangle is now located).

One more note: I use auto-layout and set limits for "tooltipLabel" to fill the screen as needed. This works as expected, as cutting / adding more text shows a tooltip of the appropriate size. But it seems to me that I need to somehow give a "hint" or "automatic" so that the text is placed in the "roundRect" part of the "tooltipLabel".

Screenshot # 1 uses this drawTextInRect method, and you can see that the full text is displayed, but overlays the “triangle” area (plus it has no inserts, which is not desirable):

    override public func drawTextInRect(rect: CGRect) {
        super.drawTextInRect(rect)
//        super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)))
    }

№2 drawTextInRect ( Height, ), , 2- 3- , 't' fit ' roundRect:

    override public func drawTextInRect(rect: CGRect) {
//        super.drawTextInRect(rect)
        super.drawTextInRect(UIEdgeInsetsInsetRect(self.roundRect, UIEdgeInsets(top: 10, left: 10, bottom: 10+self.triangleHeight, right: 10)))
    }

'drawRect':

override public func drawRect(rect: CGRect) {
    self.roundRect = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: rect.height-self.triangleHeight)
    self.triangleBezier.moveToPoint(CGPoint(x: self.roundRect.midX-self.triangleWidth/2, y: self.roundRect.maxY))
    self.triangleBezier.addLineToPoint(CGPoint(x: rect.midX, y: rect.maxY))
    self.triangleBezier.addLineToPoint(CGPoint(x: self.roundRect.midX+self.triangleWidth/2, y: self.roundRect.maxY))      

    self.triangleBezier.closePath()
    self.roundRectBezier = UIBezierPath(roundedRect: self.roundRect, cornerRadius: 5.0)
    self.roundRectBezier.appendPath(self.triangleBezier)
    self.tooltipColor.setFill()
    self.roundRectBezier.fill()

    super.drawRect(rect)
}

Screenshot # 1 Screenshot # 2

+4
1

UILabel , . .

- /: enter image description here

UITextView, : / UILabel

+1

All Articles