UILabel subclass ignores foregroundColor in Attributed string when label uses a named color

半世苍凉 提交于 2019-12-11 17:43:41

问题


I just came across a very strange problem in a custom UILabel subclass and I need some help to figure out what is going on.

The question appears to be pretty long, but this is mostly due to the images I have added to illustrate the problem as good as possible :-)

Thank you very much for your help!


The MyHTMLLabel class uses the awakeFromNib() method to parse its text property for HTML like <hightlight> tags to show the text between these tags in a a different color.

This has worked without any problems for years. Now I have updated my app to support iOS 13 dark mode and thus assigned named Colors to all controls.

If a named Color is assigned to MyHTMLLabel the text highlighting does not work any more. The complete label appears in the named color only. If a regular color is used as before (selected using the color picker) everything works as before.


BUT

The problem only arises in my existing project. I tried to illustrate the problem in a new project but everything works just fine there...

The exact same code and classes show different behaviour in my existing project an in the demo project.

Thus the source of the problem seems to be hidden somewhere in my existing project but I have absolutely no idea where...


What I have tried:

  • To make it easier to examine the problem I have removed all necessary code from MyHTMLLabel. The result is the MyLabel class which does nothing more that to replace the existing text with a colored AttributedString in awakeFromNib()
  • I have created a blank TestViewController which only contains two MyLabel instances. One instance uses a regular color while the other instance uses a named color
  • When presenting the TestViewController in a fresh demo project everything is fine: both MyLabels show the colored text
  • When presenting the TestViewController in my existing project only the MyLabel with the normal color shows the colored text while the one with the named color shows only the named color

class MyLabel: UILabel {

    override func awakeFromNib() {
        super.awakeFromNib()

        let customText = NSMutableAttributedString.init(string: "This is blue")
        customText.addAttribute(.foregroundColor, value: UIColor.blue, range: NSRange.init(location: 8, length: 4))

        attributedText = customText
    }

}

Observersions

  • The tests have been performed using the exact same code in both the demo project and my existing project. The problem only shows up in my existing project. Thus the source of the problem seems to be hidden somewhere in my existing code.
  • The existing project was created 7 years ago running in Objectiv-C and now uses Swift as well. Naturally there are thousands of differences compared to a fresh project. Problem is that I have no idea how to search for the one difference which causes the problem.
  • The problem only shows up when the colored, attributed string is assigned in awakeFromNib(). If a add another method which is triggered later (e.g. on a button tap) and assigns a colored, attributed string it is displayed just fine.
  • Thus there seems to be some kind of difference in the internal setup process bases on wether a normal or a named color is used.

Any idea or advice on how to examine this problem further?

I thought about overriding all UILabel methods in MyLabel to see which are called in the setup/init process and which is the first to use the wrong color. However there seem to be about a hundred methods which can be overwritten and the process would be very cumbersome?

Is there any better idea how to examine the problem further?

Thanks a lot!

来源:https://stackoverflow.com/questions/58660026/uilabel-subclass-ignores-foregroundcolor-in-attributed-string-when-label-uses-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!