ios - How to set both spacing between characters and strikethrough style for `UILabel`? -


i need set 2 attributes text presented uilabel: spacing between letters, , strikethrough style. based on nsattributedstringkey documentation have created following extension uilabel:

extension uilabel {     func setstrikethroughspacedtext(text: string, spacing: cgfloat?) {         var attributes: [nsattributedstringkey : any] = [:]         if let spacing = spacing {             attributes[.kern] = spacing         }         attributes[.strikethroughstyle]                    = nsnumber(integerliteral: nsunderlinestyle.stylesingle.rawvalue)         self.attributedtext = nsattributedstring(string: text,                                                  attributes: attributes)     } } 

however, seems .kern key somehow collides .strikethroughstyle key, because if specify spacing, spacing applies, not strikethrough style. if don't specify spacing (so extension not apply .kern attribute), strikethrough style works.

anyone has different way how work around bug (i assume bug)?

try this, should work
note: tested in swift 4

let label = uilabel() let stringvalue = "how to\ncontrol\nthe\nline spacing\nin uilabel" let attrstring = nsmutableattributedstring(string: stringvalue) var style = nsmutableparagraphstyle() style.linespacing = 24 // change line spacing between paragraph 36 or 48 style.minimumlineheight = 20 // change line spacing between each line 30 or 40 attrstring.addattribute(nsattributedstringkey.paragraphstyle, value: style, range: nsrange(location: 0, length: stringvalue.characters.count)) attrstring.addattribute(nsattributedstringkey.strikethroughstyle, value: 2, range: nsmakerange(0, attrstring.length)) attrstring.addattribute(nsattributedstringkey.kern, value: 2, range: nsmakerange(0, attrstring.length)) label.attributedtext = attrstring 


result:
sim 1: strike + linespacing
sim 2: strike + linespacing + character spacing

enter image description here


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -