Showing posts with label Color string. Show all posts
Showing posts with label Color string. Show all posts

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.

Color string Android


We can color string in xml file.
android:textColor="#00F"
If want to do in code.
TextView.setTextColor(Color.parseColor("#800080"));
If we want to color a string line with multicolor, we have to use SpannableString.
String text="This is multicolor text line";
SpannableString s = new SpannableString(text);
s.setSpan(new ForegroundColorSpan(Color.RED), 0, 8, 0);
s.setSpan(new ForegroundColorSpan(Color.parseColor("#008000")), 8, 15, 0);
s.setSpan(new ForegroundColorSpan(Color.parseColor("#0000ff")), 16, 27, 0);
tv.setText(s);





Run to see a multi color string android.
Now we have a string contain many lines, we want the lines have different color. Normaly, we set one line to one textView and color string textView. But use SpannableStringBuilder  we can do it in one textView.
For example, we have a string contain seven text lines, from Monday to Sunday, we want to set different colors in one textView.
Declare an arraylist.
ArrayList<String> ar = new ArrayList<String>();
Add elements.
ar.add("Monday");
ar.add("Tuesday");
ar.add("Wednesday");
ar.add("Thursday");
ar.add("Friday");
ar.add("Saturday");
ar.add("Sunday");
Use for command to loop through arraylist.
     SpannableString s1 = new SpannableString("\n");
          SpannableStringBuilder builder = new SpannableStringBuilder();
for (int i = 0; i < ar.size(); i++) {
     if (ar.get(i).equals("Monday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#FF7F50")), 0, ar
                               .get(i).length(), 0);
               builder.append(redSpannable);
               builder.append(s1);                
} else if (ar.get(i).equals("Tuesday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#0000ff")),
                               0, ar.get(i).length(), 0);
                     builder.append(redSpannable);
                     builder.append(s1);
} else if (ar.get(i).equals("Wednesday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#ff008000")),
                               0, ar.get(i).length(), 0);
                     builder.append(redSpannable);
                     builder.append(s1);
} else if (ar.get(i).equals("Thursday")) {SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#800000")),
                               0, ar.get(i).length(), 0);
          builder.append(redSpannable);
          builder.append(s1);
     } else if (ar.get(i).equals("Friday")) {
                     SpannableString redSpannable = new SpannableString(ar.get(i));
redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#00FF00")),
                               0, ar.get(i).length(), 0);
     builder.append(redSpannable);
     builder.append(s1);
     }
     else if (ar.get(i).equals("Saturday")) {
SpannableString redSpannable = new SpannableString(ar.get(i));
     redSpannable.setSpan(new ForegroundColorSpan(Color.parseColor("#FF00FF")),
                               0, ar.get(i).length(), 0);
          builder.append(redSpannable);
          builder.append(s1);
     }
else {
SpannableString redSpannable = new SpannableString(ar.get(i));
                     redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, ar
                               .get(i).length(), 0);
     builder.append(redSpannable);
     builder.append(s1);
}
}
     builder.delete(builder.length() - 1, builder.length());
     tv.setText(builder);
Press Ctrl+Shift+O to import libraries. The result will look like this.