Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Problema en la etiqueta fuerte al analizar HTML en Swift 5

Hola, tengo un problema cuando analizo un Html a NSAtributtedString en una UILabel. El problema está en el texto fuerte, el resultado es demasiado negrita (supongo que al analizar la fuente se cambia a "GibsonBold" y necesito GibsonSemibold, o incluso cambiar de Regular a Medium. Intenté cambiar la etiqueta a <b> o implementar un style, pero el resultado es siempre el mismo. También probé varias extensiones pero salí con el mismo resultado.

Esta es la cadena html:

 "<p>First text <strong> this text should be at lest Semibold</strong>. Final text.</p>"

Estoy usando estas dos extensiones para analizar:

 extension UIColor { var hexString:String? { if let components = self.cgColor.components { let r = components[0] let g = components[1] let b = components[2] return String(format: "%02X%02X%02X", (Int)(r * 255), (Int)(g * 255), (Int)(b * 255)) } return nil } } extension String { var html2Attributed: NSAttributedString? { do { guard let data = data(using: String.Encoding.utf8) else { return nil } return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print("error: ", error) return nil } } var htmlAttributed: (NSAttributedString?, NSDictionary?) { do { guard let data = data(using: String.Encoding.utf8) else { return (nil, nil) } var dict:NSDictionary? dict = NSMutableDictionary() return try (NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: &dict), dict) } catch { print("error: ", error) return (nil, nil) } } func htmlAttributed(using font: UIFont, color: UIColor, lineHeight: CGFloat) -> NSAttributedString? { do { let htmlCSSString = "<style>" + "html *" + "{" + "font-size: \(font.pointSize * 0.75)pt !important;" + "color: #\(color.hexString!) !important;" + "font-family: \(font.familyName), Helvetica !important;" + "line-height: \(lineHeight * 0.06) !important;" + "}</style> \(self)" guard let data = htmlCSSString.data(using: String.Encoding.utf8) else { return nil } return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print("error: ", error) return nil } } }

Y la implementación en la etiqueta:

 descriptionLabelText.attributedText = description.htmlAttributed(using: UIFont.gibsonRegular.withAdjustedSize(16), color: .greyscale800, lineHeight: 20.adjusted)
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!