// // CaptionTextLayoutManager.swift // Mirchi // // Created by Miro Markarian on 5/26/21. // Copyright © 2021 Mirchi Inc. All rights reserved. // import UIKit @objc @objcMembers class CaptionTextLayoutManager: NSLayoutManager { override func drawUnderline(forGlyphRange glyphRange: NSRange, underlineType underlineVal: NSUnderlineStyle, baselineOffset: CGFloat, lineFragmentRect lineRect: CGRect, lineFragmentGlyphRange lineGlyphRange: NSRange, containerOrigin: CGPoint ) { // The following is never printed and this function is never called in iOS 15.2 print("DRAWING UNDERLINE VIA drawUnderline...") let firstPosition = location(forGlyphAt: glyphRange.location).x let lastPosition: CGFloat if NSMaxRange(glyphRange) < NSMaxRange(lineGlyphRange) { lastPosition = location(forGlyphAt: NSMaxRange(glyphRange)).x } else { lastPosition = lineFragmentUsedRect( forGlyphAt: NSMaxRange(glyphRange) - 1, effectiveRange: nil).size.width } var lineRect = lineRect let height = lineRect.size.height - baselineOffset + 5 // replace your under line height lineRect.origin.x += firstPosition lineRect.size.width = lastPosition - firstPosition lineRect.size.height = height lineRect.origin.x += containerOrigin.x lineRect.origin.y += containerOrigin.y lineRect = lineRect.integral.insetBy(dx: -5, dy: -5) // Draw the background let path = UIBezierPath(roundedRect: lineRect, cornerRadius: 5) path.fill() // Draw the underline let underlineRect = CGRect(x: lineRect.origin.x, y: lineRect.origin.y + lineRect.size.height - 3, width: lineRect.size.width, height: 3) let pathBottom = UIBezierPath(roundedRect: underlineRect, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 5, height: 5)) UIColor(red: 59.0/255.0, green: 251.0/255.0, blue: 208.0/255.0, alpha: 1.0).setFill() pathBottom.fill() } }