ios - Adjust font size of NSMutableAttributedString proportional to UILabel's frame height -
in project, using swift 3.0. right using following class (uilabel subclass) adjust font size based on uilabel frame height. when uilabel frame change occurs, layoutsubviews recalculates proportional font size.
class label: uilabel { // fixme: - properties var fontsize: cgfloat = 0 var frameheight: cgfloat = 0 // fixme: - proportional font size adjustment override func layoutsubviews() { super.layoutsubviews() font = font.withsize(frame.size.height * (fontsize / frameheight)) } }
how use:
private let id: label = { let label = label() label.textalignment = .left label.numberoflines = 1 label.font = uifont.systemfont(ofsize: 17, weight: .semibold) label.textcolor = uicolor(hex: 0x212121, alpha: 1) label.fontsize = 17 label.frameheight = 20 label.clipstobounds = true return label }()
now want show part of string in uilabel bold text , remaining in regular text. have found on thread: making text bold using attributed string in swift
i using "prajeet shrestha's" extension nsmutableattributedstring.
// "prajeet shrestha's" extension extension nsmutableattributedstring { func bold(_ text:string) -> nsmutableattributedstring { let attrs:[string:anyobject] = [nsfontattributename : uifont(name: "avenirnext-medium", size: 12)!] let boldstring = nsmutableattributedstring(string:"\(text)", attributes:attrs) self.append(boldstring) return self } func normal(_ text:string)->nsmutableattributedstring { let normal = nsattributedstring(string: text) self.append(normal) return self } }
but not getting how can change font size of nsmutableattributedstring, when uilabel frame change occurs?
any appeciated.
try using label property adjustsfontsizetofitwidth , minimumscalefactor this:
label.adjustsfontsizetofitwidth = true label.minimumscalefactor = 0.2
then need increase number of lines number instead of 10
label.numberoflines = 10
Comments
Post a Comment