ios 如何制作具有多种颜色的单个UILabel

xxhby3vn  于 2023-04-13  发布在  iOS
关注(0)|答案(3)|浏览(151)

我想创建一个单一的uilabel与2种颜色。第一种颜色是黑色,另一种是蓝色。
我可以使用多个uilabel在它,但我想只有一个uilabel.有什么办法,我可以实现这一点?
这应该是输出。

下面是我的代码:

UILabel * lblPostContent = [[UILabel alloc] initWithFrame:CGRectMake((ICON_PADDING*1.5), 42, container.frame.size.width-30, 34)];
lblPostContent.numberOfLines =0;
[lblPostContent setFont:[UIFont systemFontOfSize:11]];
[lblPostContent setText:[NSString stringWithFormat:@"I just scored %d points at %@ using the iBowl app", score, LuckyStrikes]];
[container addSubview:lblPostContent];
xdyibdwo

xdyibdwo1#

你应该使用属性……像这样

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:yourString];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blackColor] range: NSMakeRange(0, TXTTOBEBLACKLENGTH)];
[text addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(TXTTOBEBLACKLENGTH, TXTTOBEBLBLUELENGTH)];
[lblPostContent setAttributedText: text];
bihw5rsg

bihw5rsg2#

为了避免计算文本各部分的长度并生成NSRange s,您可以使用appendAttributedString:从组件NSAttributedString对象组合最终的NSMutableAttributedString,如下所示:

UIColor *normalColor = [UIColor blackColor];
UIColor *highlightColor = [UIColor blueColor];
UIFont *font = [UIFont systemFontOfSize:12.0];        
NSDictionary *normalAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:normalColor};
NSDictionary *highlightAttributes = @{NSFontAttributeName:font, NSForegroundColorAttributeName:highlightColor};

NSAttributedString *normalText = [[NSAttributedString alloc] initWithString:@"Normal " attributes:normalAttributes];
NSAttributedString *highlightedText = [[NSAttributedString alloc] initWithString:@"Highlighted" attributes:highlightAttributes];

NSMutableAttributedString *finalAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:normalText];
[finalAttributedString appendAttributedString:highlightedText];

self.label.attributedText = finalAttributedString;
gtlvzcf8

gtlvzcf83#

下面是AttributedString的扩展函数:

extension NSMutableAttributedString {

    func setColorForText(textToFind: String, withColor color: UIColor) {
        let range: NSRange = self.mutableString.range(of: textToFind, options: .caseInsensitive)
        self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range)
    }

}

使用标签试试:

let label = UILabel()
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200)
let first = "89"
let second = "Lucky Strikes"
let third = "iBowl app"
let stringValue = "I just scored \(first) points at \(second) using the \(third)"  // or direct assign single string value like "firstsecondthird"
label.textColor = UIColor.lightGray
label.numberOfLines = 0
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
attributedString.setColorForText(textToFind: first, withColor: UIColor.blue)   // use variable for string "89"
attributedString.setColorForText(textToFind: second, withColor: UIColor.blue) // or direct string like this "Lucky Strikes"
attributedString.setColorForText(textToFind: third, withColor: UIColor.blue)
label.font = UIFont.systemFont(ofSize: 26)
label.attributedText = attributedString
self.view.addSubview(label)

结果如下:

相关问题