Advertisement
Don_Mag

Untitled

Jun 6th, 2023
1,465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.06 KB | None | 0 0
  1. class AttribVC: UIViewController {
  2.    
  3.     override func viewDidLoad() {
  4.         super.viewDidLoad()
  5.        
  6.         let g = view.safeAreaLayoutGuide
  7.        
  8.         var theLabels: [UILabel] = []
  9.        
  10.         var prevLabel: UILabel!
  11.        
  12.         for _ in 0..<6 {
  13.             let v = UILabel()
  14.             v.translatesAutoresizingMaskIntoConstraints = false
  15.             view.addSubview(v)
  16.             v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0).isActive = true
  17.             v.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0).isActive = true
  18.             if prevLabel == nil {
  19.                 v.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0).isActive = true
  20.             } else {
  21.                 v.topAnchor.constraint(equalTo: prevLabel.bottomAnchor, constant: 8.0).isActive = true
  22.             }
  23.             theLabels.append(v)
  24.             prevLabel = v
  25.         }
  26.        
  27.         var attStr: NSMutableAttributedString!
  28.         var attributes: [NSAttributedString.Key: Any] = [:]
  29.         var rangeStr: String = ""
  30.         var termRange: NSRange!
  31.        
  32.         let html: String = "<p>See additional information</p>"
  33.         guard let encodedText = html.data(using: .utf16) else { return }
  34.        
  35.         guard let attributedString = try? NSMutableAttributedString(data: encodedText, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
  36.         else { return }
  37.        
  38.         rangeStr = "See additional information"
  39.  
  40.         termRange = attributedString.mutableString.range(of: rangeStr)
  41.        
  42.         // default attributed string
  43.         attStr = NSMutableAttributedString(attributedString: attributedString)
  44.         theLabels[0].attributedText = attStr
  45.        
  46.         // .strokeWidth and .foregroundColor
  47.         //  results in a black "outline"
  48.         attStr = NSMutableAttributedString(attributedString: attributedString)
  49.         attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red]
  50.         attStr.addAttributes(attributes, range: termRange)
  51.         theLabels[1].attributedText = attStr
  52.        
  53.         // .strokeWidth and .foregroundColor and .strokeColor
  54.         //  results in a "bolded" effect
  55.         attStr = NSMutableAttributedString(attributedString: attributedString)
  56.         attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red, .strokeColor: UIColor.red]
  57.         attStr.addAttributes(attributes, range: termRange)
  58.         theLabels[2].attributedText = attStr
  59.        
  60.         rangeStr = "additional"
  61.         termRange = attributedString.mutableString.range(of: rangeStr)
  62.        
  63.         // default attributed string
  64.         attStr = NSMutableAttributedString(attributedString: attributedString)
  65.         theLabels[3].attributedText = attStr
  66.        
  67.         // .strokeWidth and .foregroundColor
  68.         //  results in a black "outline"
  69.         attStr = NSMutableAttributedString(attributedString: attributedString)
  70.         attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red]
  71.         attStr.addAttributes(attributes, range: termRange)
  72.         theLabels[4].attributedText = attStr
  73.        
  74.         // .strokeWidth and .foregroundColor and .strokeColor
  75.         //  results in a "bolded" effect
  76.         attStr = NSMutableAttributedString(attributedString: attributedString)
  77.         attributes = [.strokeWidth: -3.0, .foregroundColor: UIColor.red, .strokeColor: UIColor.red]
  78.         attStr.addAttributes(attributes, range: termRange)
  79.         theLabels[5].attributedText = attStr
  80.        
  81.     }
  82.    
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement