Get each line of text in a UILabel

前端 未结 1 710
说谎
说谎 2020-12-19 11:50

I\'m trying to add each line in a UILabel to an array, but the code I\'m using doesn\'t appear to be terribly accurate.

func getLinesArrayOfStri         


        
相关标签:
1条回答
  • 2020-12-19 12:01

    So it appears to be something with UILabel and not something wrong with the function you are using. It was my suspicion that a CATextLayer would render the lines how they are returned from that method and I found out sadly :( that I am right.

    Here is a picture of my results:

    The red is the exact code you used to create your UILabel.

    The green is a CATextLayer with all of the same characteristics of the UILabel from above including font, fontsize, and frame size.

    The yellow is a subclassed UIView that is replacing its own layer and returning a CATextLayer. I am attaching it below. You can continue to build it out to meet your needs but I think this is the real solution and the only one that will have the get lines matching the visible lines the user sees. If you come up with a better solution please let me know.

    import UIKit
    
    class AGLabel: UIView {
    
        var alignment : String = kCAAlignmentLeft{
            didSet{
                configureText()
            }
        }
        var font : UIFont = UIFont.systemFont(ofSize: 16){
            didSet{
                configureText()
            }
        }
        var fontSize : CGFloat = 16.0{
            didSet{
                configureText()
            }
        }
        var textColor : UIColor = UIColor.black{
            didSet{
                configureText()
            }
        }
    
        var text : String = ""{
            didSet{
                configureText()
            }
        }
    
    
        override class var layerClass: AnyClass {
            get {
                return CATextLayer.self
            }
        }
    
        func configureText(){
            if let textLayer = self.layer as? CATextLayer{
                textLayer.foregroundColor = textColor.cgColor
                textLayer.font = font
                textLayer.fontSize = fontSize
                textLayer.string = text
                textLayer.contentsScale = UIScreen.main.scale
                textLayer.contentsGravity = kCAGravityCenter
                textLayer.isWrapped = true
            }
        }
    }
    

    You should also check out Core-Text-Label on GitHub. It renders exactly as the CATextLayers do and would match the return of the get lines. It won't work for my particular needs as I need mine to be resizable and it crashes but if resizing is not need then I would check it out.

    Finally I am back again and it appears that it could be a problem of word wrap that was started in iOS 11 where they do not leave an orphan word on a line.

    0 讨论(0)
提交回复
热议问题