-1

I have a problem with this code and I do not know what I am doing wrong. I tried everything in Playground first and it works like a charm. Assigning this to the UILabel it just not working.

let nameSurname = "\(postAddSetup.nameSurname.text!)"
let checkIn = " - \(setLocationPlace), \(setLocationCity)"

var string = postAddSetup.nameSurname.text
string = "\(nameSurname) \(checkIn)"

let boldUsername = NSMutableAttributedString(string: string!)
boldUsername.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 14), range: (string! as NSString).range(of: nameSurname))
let normalCheckIn = NSMutableAttributedString(string: string!)
normalCheckIn.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 11), range: (string! as NSString).range(of: checkIn))
normalCheckIn.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: (string! as NSString).range(of: checkIn))
print(string!)

postAddSetup.nameSurname.text = string!

I practically have a label that is getting a text from 2 strings. Those strings I want to be displayed with different colors and fonts. It works in Playground but not in the ViewController. Can anybody help, please?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Marian Petrisor
  • 262
  • 2
  • 19
  • it might be obvious, but are you sure that in the View Controller this code has been executed? – Ahmad F Oct 29 '17 at 14:44
  • 2
    Define "not working". That's a useless description. In what way exactly is the posted code not working? Errors? Crashes? Unexpected results? [Edit] your question to clearly indicate specific details about your issue. – rmaddy Oct 29 '17 at 14:48
  • rmaddy - If you would have read the whole question and code, you would have understood that I am a beginner. And I didn't come on this website for someone to be that rude. Probably I didn't say it right, my bad. But next time, do not help if you want to be rude about it. – Marian Petrisor Oct 29 '17 at 15:25
  • 2
    MarianPetrisor: please consider not biting the hand that feeds you. @rmaddy's comment was anything but rude, was simply asking for clarification of your current problem, and if followed in future questions, will lead to better and quicker answers. Please look at it from that light, as a positive suggestion that will help you now and later if followed. – Hovercraft Full Of Eels Oct 29 '17 at 15:31
  • I am looking for the hand that helps me. I am a beginner! When you say "useless" in a sentence, you are being rude. And somebody did bother to look at the question to see what is wrong with my code and also gave me an answer and an explanation so I can learn from my mistakes, so it wasn't that useless. I call that experience! Thank you, Francesco! – Marian Petrisor Oct 29 '17 at 15:39
  • Again, you're misunderstanding things. Yes, Francesco was able to find your error, but don't you think that it is in your best interest to make it as easy as possible for volunteers here to find the error? Again, if not for this question, then for future questions? And truthfully tell me, how does "not working" help me to understand what your actual error is? Is it in fact a "useful" statement to make? Would it be more useful to explain how it's not working? What is rude with calling a statement (and not calling you the poster) "useless"? Is asking for clarification rude? – Hovercraft Full Of Eels Oct 29 '17 at 15:49
  • I am here asking questions not to be made as simple as possible.Even though showing me the results it would clarify the way it works.I am asking a question because I do not know, and I think this is the propose of the website, to help each other. It's the way it was said, and it was rude.I think people can ask politely for clarification, as we do not know each other.This is networking, people from all around the globe helping each other to get past some gaps in their knowledge.Yes, my question probably didn't clarify everything I asked,but I am still a beginner. Approachable people would help. – Marian Petrisor Oct 29 '17 at 16:27
  • 1
    Please let me share some advice I was given when I was new here: grow a thicker skin. The comments didn't criticize you, but asked for clarification on the question. Instead of complaining about them, consider providing the requested information. Help answering volunteers by making it easy to help you. – Modus Tollens Oct 29 '17 at 16:34
  • You are correct! – Marian Petrisor Oct 29 '17 at 17:14
  • .... but I wasn't it seems. :( – Hovercraft Full Of Eels Oct 29 '17 at 17:19
  • 1
    Hovercraft... You are also right! I overreacted :) Thank you! – Marian Petrisor Oct 29 '17 at 17:28

1 Answers1

1

In your example you have two different attributed string created from the same original string and you are passing your string without attributes to the label text property. Instead you can create a unique attributed string and you can use it with attributedText property of your label:

var string = postAddSetup.nameSurname.text
    string = "\(nameSurname) \(checkIn)"

    let attributedString = NSMutableAttributedString(string: string!)
    attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 14), range: (string! as NSString).range(of: nameSurname))
    attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 11), range: (string! as NSString).range(of: checkIn))
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: (string! as NSString).range(of: checkIn))

    postAddSetup.nameSurname.attributedText = attributedString
Francesco Deliro
  • 3,899
  • 2
  • 19
  • 24
  • You are so right Francesco. Thank you for this! It's the first time I am using attributedText and know I understand what is going on. Thank you! – Marian Petrisor Oct 29 '17 at 15:21
  • You are welcome! Happy to help ;) And following your comment I will edit my answer to give a better explanation, thanks! – Francesco Deliro Oct 29 '17 at 17:35