您好,下面是我需要使用的一种视图UILabel
。我听说过NSAttributedString
但不确定如何将其用于动态文本加载。
这里的整个文本字体是Roboto-Light。但是,我必须替换API响应中前两位医生的文字“ Andrew Murphy,John Smith”,并从API中获取“ 23位医生”的计数,以便在该标签中进行相应调整。您可以看到的文本颜色取决于文本是恒定的还是动态的。我不确定如何实现。因此,确实欢迎一些代码片段。
谢谢!
您可以像这样使用NSMutableAttributeString和addAttribute:value:range;
//Your entry string
NSString *myString = @"I have to replace text 'Dr Andrew Murphy, John Smith' ";
//Create mutable string from original one
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:myString];
//Fing range of the string you want to change colour
//If you need to change colour in more that one place just repeat it
NSRange range = [myString rangeOfString:@"John Smith"];
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];
//Add it to the label - notice its not text property but it's attributeText
label.attributedText = attString;
希望有帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句