ios - UILabel underline text with high characters below baseline -


i'd make underlined text uilabel. now, i'm using:

self.lbvalue.attributedtext = nsattributedstring(string: value, attributes:      [nsunderlinestyleattributename : nsunderlinestyle.stylesingle.rawvalue]) 

it draw single line under text, that's alright. however, when character high (and fall below base line), crops line , makes ugly blank space between line, this:

high characters

as can see, there's tiny dot below characters. that's underline go. how make solid line , overlaps characters? this:

expected output

i've tried other style (dash, dot, thick, solid...) none of them work expected.

this can achieved in other better way well.i show example-:

1) add label and, uiview below label(which work underline).

2) provide appropriate constraints.

check image below-:

label constraints-:

enter image description here

underline view constraints-:

enter image description here

now in controller class add outlets. and, add width nslayoutconstraint underline uiview.

as suppose label width can change dynamically give underline label(width) intrinsiccontentsize . adjust underline label width based upon content in uilabel.

controller class-:

import uikit  class underlineviewcontroller: uiviewcontroller {       @iboutlet weak var mylabel: uilabel!     @iboutlet weak var underlinewidth: nslayoutconstraint!     @iboutlet weak var underline: uiview!       override func viewdidload() {         super.viewdidload()         // set text label         mylabel.text = "what's next in game of thrones"         // make underline view width inttrinsic         underlinewidth.constant = mylabel.intrinsiccontentsize.width         // additional setup after loading view.      }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }  } 

output-:

enter image description here

enter image description here

hope helps you.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -