swift3 - How to change only variable color in a print label -


i want change color variable in tuple print this

result.text = ("\(var1) candy  " + " of: \(var2) blablabla") 

now, how can change color of "var1" , "var2", inspector set red color label, it's impossible change color variable.

thanks lot

what have create attributed string each color (let's blue , yellow):

let t : nsattributedstring = nsattributedstring(string: "hi", attributes: [nsforegroundcolorattributename : uicolor.blue])  let t2 : nsattributedstring = nsattributedstring(string: " there", attributes: [nsforegroundcolorattributename : uicolor.yellow]) 

if want join them together:

let final = nsmutableattributedstring(attributedstring: t)  final.append(t2) 

update

so in case, since want color 2 different sections of resulting string, you'd want use nsrange approach. method this:

func colorthevariables(_ var1value: string,_ var2value: string) {          let middle = " candy of: ".characters.count          let value = "\(var1value) candy of : \(var2value)"          let text = nsmutableattributedstring(string: value)          let range1 = nsrange(location: 0, length: var1value.characters.count)         let range2 = nsrange(location: range1.length + middle, length: var2value.characters.count)          text.addattribute(nsforegroundcolorattributename, value: var1color, range: range1)         text.addattribute(nsforegroundcolorattributename, value: var2color, range: range2)          result.attributedtext = text     } 

should give coloring want var1 , var 2. it's important apply text attributedtext of label, not text. printing result.text should give content of label, printing result.attributedtext give weird nsattributed string print.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -