您好我想知道如何以此形式显示uilabel文字?
@JoinColumn(name="PARENT_ID")
在实际的debuging期间,更新了Mr.Str =" Fashion Gadgets Travel"但高亮文本不会突出显示所有内容,如下图所示。我想要的结果是上面的图像。
答案 0 :(得分:0)
@IBOutlet weak var myInterestsLbl:UILabel?
let str = "Fashion Gadgets Travel";
let updatedStr = str.replacingOccurrences(of: ",", with: " ");
self.myInterestsLbl!.text = updatedStr;
//Highlight Text
let string = self.myInterestsLbl!.text;
if string?.range(of:"Fashion") != nil {
let fashion = "Fashion";
let range = (string! as NSString).range(of: fashion);
let attributedText = NSMutableAttributedString.init(string: string!);
attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor(red: 0.98, green: 0.84, blue: 0.04, alpha: 1.0) , range: range);
self.myInterestsLbl!.attributedText = attributedText;
}
if string?.range(of:"Gadgets") != nil {
let fashion = "Gadgets";
let range = (string! as NSString).range(of: fashion);
let attributedText = self.myInterestsLbl!.attributedText as? NSMutableAttributedString; // You did mistake here. You were initialising it via String variable instead of changed attributed string.
attributedText?.addAttribute(NSBackgroundColorAttributeName, value: UIColor(red: 0.98, green: 0.84, blue: 0.04, alpha: 1.0) , range: range);
self.myInterestsLbl!.attributedText = attributedText;
}