ios - Check if line break in the UILabel -
this question has answer here:
i have problem using uilabel.
set line count 0. when label receives big string
value, occurred line break, problem when height of label, height comes smaller should, comes value line only, shown string
2 lines.
i have tried use code below, no avail.
let originalheight = label.frame.size.height label.sizetofit() //or label.layoutifneeded() let newheight = label.frame.size.height
originalheight
, newheight
equals.
if line break made using character \n
, above code works.
// mark: - getting dynamic height of string
convenience init(for bodytext: string, width: cgfloat) { if (bodytext.characters.count ?? 0) <= 0 { return 0 } let cellfont = uifont(name: "helvetica", size: 15.0) //uifont *cellfont = [uifont systemfontofsize:13.f]; var paragraphstyle = nsmutableparagraphstyle.default.mutablecopy() paragraphstyle.linespacing = 1 paragraphstyle.lineheightmultiple = 1.0 /// set line break mode paragraphstyle.linebreakmode = nslinebreakbywordwrapping /// set text alignment paragraphstyle.alignment = .natural let attributesdictionary: [anyhashable: any] = [ nsfontattributename : cellfont!, nsparagraphstyleattributename : paragraphstyle ] let expectedlabelsize = bodytext.boundingrect(with: cgsize(width: width, height: flt_max), options: .useslinefragmentorigin, attributes: (attributesdictionary as? [string : any] ?? [string : any]()), context: nil).size
Comments
Post a Comment