0

In Android, when I set my text as below

my_text.text = Html.fromHtml("<B>From</B> Someone", FROM_HTML_MODE_COMPACT)

I will get

enter image description here

And when I just change the attribute in the TextView as below

        android:textColor="#F40"
        android:textSize="24sp"
        android:fontFamily="serif-monospace"

I will get

enter image description here

Which is cool, as I could control my attribute from the XML, while the text could come from some end point services (to control to either bold or not bold the text).

I could do similar task on iOS using the following code

let htmlString = "<B>From</B> someone"
let data = htmlString.data(using: String.Encoding.unicode)! 
let attrStr = try? NSAttributedString( 
  data: data,
  options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  documentAttributes: nil)
myLabel.attributedText = attrStr

It always resulted as

enter image description here

It no longer honor the attribute set from the interface builder. Is there a way where I could still have the Label keep the attributes (font type, size, color) set by the interface builder, while the text set to it is will decide what to bold or not using <b></b> tag?

Elye
  • 53,639
  • 54
  • 212
  • 474

0 Answers0