Thursday, February 21, 2019

Color string Swift

Normally, to color text in label, we use
label.textColor = UIColor.blueColor()
To make text with multi color string, use NSMutableAttributedString.
let text="This is example text"
let at =  NSMutableAttributedString(string: text,attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)])
        at.addAttribute(NSForegroundColorAttributeName, value:UIColor.redColor(), range: NSMakeRange(0,4))
        at.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:6,length:5))
       la.attributedText = at

Next, we want to color multi rows string, each row one color, in only one label, by use
NSMutableAttributedString.
Declare an array, add elements.
var day=[String](count: 7, repeatedValue: " ")
        day[0]="Monday"
        day[1]="Tuesday"
        day[2]="Wednesday"
        day[3]="Thursday"
        day[4]="Friday"
        day[5]="Saturday"
        day[6]="Sunday"
Declare colors.
let color1 = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color2 = [NSForegroundColorAttributeName: UIColor.brownColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color3 = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color4 = [NSForegroundColorAttributeName: UIColor.orangeColor(),NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color5 = [NSForegroundColorAttributeName: UIColor.grayColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color6 = [NSForegroundColorAttributeName: UIColor.blueColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let color7 = [NSForegroundColorAttributeName: UIColor.cyanColor(), NSFontAttributeName: UIFont.systemFontOfSize(17)]
let at = NSMutableAttributedString()
var p = NSMutableAttributedString()
Set text gravity to left.
let pa = NSMutableParagraphStyle()
        pa.alignment = .Left
Now use for loop to set color each row.
for i in0..<day.count{
if day[i] == "Monday" {
        p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color1)
            }
elseif day[i] == "Tuesday" {
        p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color2)
            }
elseif day[i] == "Wednesday" {
        p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color3)
                          }
elseif day[i] == "Thursday" {
        p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color4)
                           }
elseif day[i] == "Friday"{
       p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color5)
                            }
elseif day[i] == "Saturday"{
        p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color6)
            }
else {
      p = NSMutableAttributedString(string: thu[i] + "\n", attributes: color7)
            }
 at.appendAttributedString(p)
at.addAttribute(NSParagraphStyleAttributeName,value:pa, range:NSMakeRange(0,1))
 }
label.numberOfLines = 0
lable.attributedText = at
label. sizeToFit()
Run to see result.
la.numberOfLines = 0 make label has many lines row.

label.sizeToFit() make label hight enough.

No comments:

Post a Comment