class AttribVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let g = view.safeAreaLayoutGuide
var theLabels: [UILabel] = []
var prevLabel: UILabel!
for _ in 0..<6 {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(v)
v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0).isActive = true
v.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0).isActive = true
if prevLabel == nil {
v.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0).isActive = true
} else {
v.topAnchor.constraint(equalTo: prevLabel.bottomAnchor, constant: 8.0).isActive = true
}
theLabels.append(v)
prevLabel = v
}
var attStr: NSMutableAttributedString!
var attributes: [NSAttributedString.Key: Any] = [:]
var rangeStr: String = ""
var termRange: NSRange!
let html: String = "<p>See additional information</p>"
guard let encodedText = html.data(using: .utf16) else { return }
guard let attributedString = try? NSMutableAttributedString(data: encodedText, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
else { return }
rangeStr = "See additional information"
termRange = attributedString.mutableString.range(of: rangeStr)
// default attributed string
attStr = NSMutableAttributedString(attributedString: attributedString)
theLabels[0].attributedText = attStr
// .strokeWidth and .foregroundColor
// results in a black "outline"
attStr = NSMutableAttributedString(attributedString: attributedString)
attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red]
attStr.addAttributes(attributes, range: termRange)
theLabels[1].attributedText = attStr
// .strokeWidth and .foregroundColor and .strokeColor
// results in a "bolded" effect
attStr = NSMutableAttributedString(attributedString: attributedString)
attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red, .strokeColor: UIColor.red]
attStr.addAttributes(attributes, range: termRange)
theLabels[2].attributedText = attStr
rangeStr = "additional"
termRange = attributedString.mutableString.range(of: rangeStr)
// default attributed string
attStr = NSMutableAttributedString(attributedString: attributedString)
theLabels[3].attributedText = attStr
// .strokeWidth and .foregroundColor
// results in a black "outline"
attStr = NSMutableAttributedString(attributedString: attributedString)
attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red]
attStr.addAttributes(attributes, range: termRange)
theLabels[4].attributedText = attStr
// .strokeWidth and .foregroundColor and .strokeColor
// results in a "bolded" effect
attStr = NSMutableAttributedString(attributedString: attributedString)
attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red, .strokeColor: UIColor.red]
attStr.addAttributes(attributes, range: termRange)
theLabels[5].attributedText = attStr
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎