0

I followed this and could outline my UILabel. But I am struggling to center it. The code already says that it should be centered but nothing happens. I did center the UILabel with Attribute Inspector.

enter image description here

extension UILabel {

func addTextOutline(usingColor outlineColor: UIColor, outlineWidth: CGFloat) {
    class OutlinedText: UILabel{
        var outlineWidth: CGFloat = 0
        var outlineColor: UIColor = .clear
        
        override public func drawText(in rect: CGRect) {
            let shadowOffset = self.shadowOffset
            let textColor = self.textColor
            
            let c = UIGraphicsGetCurrentContext()
            c?.setLineWidth(outlineWidth)
            c?.setLineJoin(.round)
            c?.setTextDrawingMode(.stroke)
            self.textAlignment = .center
            self.textColor = outlineColor
            super.drawText(in:rect)
            
            c?.setTextDrawingMode(.fill)
            self.textColor = textColor
            self.shadowOffset = CGSize(width: 0, height: 0)
            super.drawText(in:rect)
            
            self.shadowOffset = shadowOffset
        }
    }
    
    let textOutline = OutlinedText()
    let outlineTag = 9999
    
    if let prevTextOutline = viewWithTag(outlineTag) {
        prevTextOutline.removeFromSuperview()
    }
    
    textOutline.outlineColor = outlineColor
    textOutline.outlineWidth = outlineWidth
    textOutline.textColor = textColor
    textOutline.font = font
    textOutline.text = text
    textOutline.center = center
    textOutline.tag = outlineTag
    
    sizeToFit()
    addSubview(textOutline)
    textOutline.frame = CGRect(x: -(outlineWidth / 2), y: -(outlineWidth / 2),
                               width: bounds.width + outlineWidth,
                               height: bounds.height + outlineWidth)
}

}

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
submariner
  • 308
  • 1
  • 5
  • 23
  • Wouldn't it be easier to just change the `UILabel` to the `OutlinedText` that you wrote? Rather than creating an `OutlinedText` on top of an existing `UILabel` and trying to centre it. – Sweeper Feb 26 '23 at 14:29
  • To be honest I don't even know why it shows fitness two times. If I remove the label on the back it also removes the label on the front. Somehow they are connected. I use this code to add the outlined Text: fitnessLabel.addTextOutline(usingColor: .white, outlineWidth: 6) – submariner Feb 26 '23 at 14:30
  • Because you added `OutlinedText` as a subview of the label you are outlining. I would advise that you actually *understand* what the code is doing before running it... In any case, that is not what I meant by my first comment. – Sweeper Feb 26 '23 at 14:34
  • I try to understand it since a few hours. So if you could help it would be great. – submariner Feb 26 '23 at 14:36
  • Rather than having a `addTextOutline` method, how about making `fitnessLabel` an instance of `OutlinedText` instead? Of course, move the declaration of `OutlinedText` out of the method first. – Sweeper Feb 26 '23 at 14:38
  • Ok great and how can I make fitnessLabel an instance of OutlinedText? – submariner Feb 26 '23 at 14:40
  • I appreciate you providing the code, but it doesn't sound like you did any minimization or analysis of the code's functional behavior. Reducing the code as @Sweeper suggested is needed to understand the layout the problem. – benc Feb 26 '23 at 21:04

0 Answers0