I am trying to set custom/hex color of attributed string, But its not working. If I am setting system color like UIColor.red
than its working, But If I set UIColor.init(hexString: "00008B")
Than its not working.
My code snippet is-
let string = "\(model.username) " + model.commentTitle as NSString
let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Kohinoor Bangla", size: 15) ?? UIFont.systemFont(ofSize: 15.0)])
let rangeUsername = (string as NSString).range(of: model.username)
let rangeCommentText = (string as NSString).range(of: model.commentTitle)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.init(hexString: "00008B"), range: rangeUsername) // Not Working
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemBlue.withAlphaComponent(0.5), range: rangeCommentText) // Its Working
let boldFontAttribute = [NSAttributedString.Key.font: UIFont(name: "Campton Bold", size: 16)]
// Part of string to be bold
attributedString.addAttributes(boldFontAttribute as [NSAttributedString.Key : Any], range: string.range(of: model.username))
cell.lblComment.attributedText = attributedString