malcommac / SwiftRichString

👩‍🎨 Elegant Attributed String composition in Swift sauce
MIT License
3.1k stars 211 forks source link

Strikethrough is not working on iOS 14 #122

Closed darubhaai closed 3 years ago

darubhaai commented 4 years ago

Thank you for this excellent handy library for text manipulation. It have really made most of my text management easy in a jiffy. But I am seeing a broken feature in iOS 14. On iOS 14 Strikethrough attribute is not working when:

I have added a sample controller code below which tries to set XML formatted Attributed String to a button's title

class ViewController: UIViewController {

@IBOutlet weak var testButton: UIButton!

var styleXML: StyleXML = {
    let baseStyle  = Style {
        $0.font = UIFont.systemFont(ofSize: 17, weight: .regular)
        $0.color = "#DD0000"
        $0.alignment = .center
    }

    // Derives baseStyle and updates font weight
    let boldStyle =  baseStyle.byAdding {
        $0.font = UIFont.systemFont(ofSize: 17, weight: .bold)
    }

    // Derives boldStyle and updates font weight, color and adds a strikethrough
    let strikeStyle = boldStyle.byAdding {
        $0.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
        $0.color = "#00DD00"
        $0.strikethrough = (.single, "#0000BB")
    }

    let boldStrikeStyleGroup = StyleXML(base: baseStyle, ["bold": boldStyle, "strike": strikeStyle])
    return boldStrikeStyleGroup
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // Create a test string and set it button's attributed text
    let attributedString = "<strike>15.99</strike> Buy now <bold>10.99</bold>".set(style: styleXML)
    testButton.setAttributedTitle(attributedString, for: .normal)
}

}

Output of the following in iOS 13 and iOS 14 devices

iOS 13.2 on Simulator. The result is same on an actual device.

iPhone11ProMax_iOS13 2_Simulator

iOS 14 latest beta, iPhone 8 Plus iPhone8Plus_iOS14_Latest_Beta

Kindly update and resolve. I appreciate the efforts made to create this Library. Using in almost every project i have worked.

Ostroverkhov commented 3 years ago

I fixed it like this: $0.baselineOffset = 0

darubhaai commented 3 years ago

Wow. This worked like cherry on cake. Thanks