0

I have aUILabel which has attributed text set like

Good morning \n Alex

It shows as 2 lines in UILabel. I want to show this and after few seconds remove the Good morning part and just show the Alex part.

Is this possible to do? Please help me

user1960169
  • 3,533
  • 12
  • 39
  • 61

3 Answers3

0

You can create a NSTimer - Documentation for number of seconds and after that timer fires, you would simply change the text of the UILabel

textLabel.attributedText = "Alex"

inside of the completion block of the NSTimer

Martin
  • 726
  • 1
  • 9
  • 11
0

You could use GCD

let delayInSeconds = 4.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) {

     textLabel.attributedText = "Alex"


 }
Ivan Skrobot
  • 311
  • 3
  • 7
0

Use this function and pass your delay and update the attributed text:-

func delay(_ delay: Double, closure: @escaping () -> Void) {
        DispatchQueue.main.asyncAfter(
            deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
    }

func updateAttributedText() {
  delay(1) { 
   self.textLabel.attributedText = NSAttributed(string: "Alex")
  }
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • `self.textLabel.attributedText = "Alex"` That line should show at least a warning (if not a crash), because you are assigning a String where you should assign a NSAttributedString. – Larme Jul 28 '17 at 15:20