如何在uilabel中为字母设置不同的颜色
How to set different color for Alphabets in uilabel
我想为 uilabel 中的字母设置灰色。如何在 NSMutableAttributedString 中设置范围值。
示例:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"1.2 Sun - 3.4 Mon"];
[attrString beginEditing];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range: ]; // how to set range here for alphabets
[attrString endEditing];
试试这个。
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"1.2 Sun - 3.4 Mon"];
if([[attrString string] rangeOfString:@"Sun"].location != NSNotFound)
{
NSRange range2 = [[attrString string] rangeOfString:@"Sun"];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range2];
}
if([[attrString string] rangeOfString:@"Mon"].location != NSNotFound)
{
NSRange range2 = [[attrString string] rangeOfString:@"Mon"];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range2];
}
编码愉快:)
我想为 uilabel 中的字母设置灰色。如何在 NSMutableAttributedString 中设置范围值。
示例:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"1.2 Sun - 3.4 Mon"];
[attrString beginEditing];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range: ]; // how to set range here for alphabets
[attrString endEditing];
试试这个。
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"1.2 Sun - 3.4 Mon"];
if([[attrString string] rangeOfString:@"Sun"].location != NSNotFound)
{
NSRange range2 = [[attrString string] rangeOfString:@"Sun"];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range2];
}
if([[attrString string] rangeOfString:@"Mon"].location != NSNotFound)
{
NSRange range2 = [[attrString string] rangeOfString:@"Mon"];
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range2];
}
编码愉快:)