如何将文字对齐标签底部?

时间:2020-11-05 14:31:08

标签: swift sprite-kit uilabel

我以编程方式创建了标签,这对我的动画场景很重要。动画场景获取用户输入的文本,并通过打字机将其显示在场景中,例如标签内的动画。标签高度决定了我可以添加多少文本,高度越大,文本越多。由于文本以打字机的形式显示在场景中,因此当添加新行时,前一行将向上移动。

如何在标签底部对齐文本?我通过stackoverflow回顾了一堆类似的问题,但是没有方法没有故事板就可以完成。

func copyWithPacing(textToUse:String)->(){
        let TempTextHolder = textToUse
        var value: [Substring] = []
        var value1: [String] = []
        
        func updateAndAnimateSceneText () {
            
            let SceneMainlabel = UILabel(frame: CGRect(x: 400 , y: -70, width: 375, height: 500))
            SceneMainlabel.center = CGPoint(x: 190, y: 0)
            SceneMainlabel.textAlignment = .center
            SceneMainlabel.textColor = .black
            SceneMainlabel.text = ""
            SceneMainlabel.font = UIFont.boldSystemFont(ofSize: SceneMainlabel.font.pointSize)
            SceneMainlabel.numberOfLines = 5
            SceneMainlabel.lineBreakMode = .byWordWrapping
            
            print("text is finished compiling")
            self.view?.addSubview(SceneMainlabel)
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1){
                let oneSpace = " "
                let twoSpacewithComma = " , "
                let oneSpacewithComma = " ,"
                let updateArray = TempTextHolder.replacingOccurrences(of: oneSpace, with: twoSpacewithComma)
                let splitArray = updateArray.components(separatedBy: oneSpacewithComma)
                
                //Delays
                for i in splitArray {
                    value.append("\(i)")
                    // SceneMainlabel.text! += "\(i)"
                    let count = SceneMainlabel.text!.components(separatedBy: " ").count
                    
                    //  delay at the 6th word space
                    if count == 6 {
                        print(count)
                        print(SceneMainlabel.text!)
                        RunLoop.current.run(until: Date()+0)
                        for b in i {
                            value1.append("\(b)")
                            SceneMainlabel.text! += "\(b)"
                            RunLoop.current.run(until: Date()+0.01)
                        }
                         
                    } else {
                        RunLoop.current.run(until: Date()+0.03)
                        for b in i {
                            value1.append("\(b)")
                            SceneMainlabel.text! += "\(b)"
                            RunLoop.current.run(until: Date()+0.05)
                        }
                        
                    }
                }
            }
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 8){
                SceneMainlabel.removeFromSuperview()
                print("labeled remove")
            }
        }
        return updateAndAnimateSceneText ()
        
    }

0 个答案:

没有答案