Adding space/padding to a UILabel

I have tried with it on Swift 4.2, hopefully it work for you! @IBDesignable class PaddingLabel: UILabel { @IBInspectable var topInset: CGFloat = 5.0 @IBInspectable var bottomInset: CGFloat = 5.0 @IBInspectable var leftInset: CGFloat = 7.0 @IBInspectable var rightInset: CGFloat = 7.0 override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top: topInset, left: leftInset, … Read more

UILabel text margin [duplicate]

I solved this by subclassing UILabel and overriding drawTextInRect: like this: – (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } Swift 3.1: override func drawText(in rect: CGRect) { let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5) super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) } Swift 4.2.1: override func drawText(in rect: CGRect) … Read more

Figure out size of UILabel based on String in Swift

Use an extension on String Swift 3 extension String { func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat { let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude) let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) return ceil(boundingBox.height) } func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat { let constraintRect = CGSize(width: .greatestFiniteMagnitude, … Read more

Bold & Non-Bold Text In A Single UILabel?

Update In Swift we don’t have to deal with iOS5 old stuff besides syntax is shorter so everything becomes really simple: Swift 5 func attributedString(from string: String, nonBoldRange: NSRange?) -> NSAttributedString { let fontSize = UIFont.systemFontSize let attrs = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: fontSize), NSAttributedString.Key.foregroundColor: UIColor.black ] let nonBoldAttribute = [ NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize), ] let … Read more

How do I make an attributed string using Swift?

This answer has been updated for Swift 4.2. Quick Reference The general form for making and setting an attributed string is like this. You can find other common options below. // create attributed string let myString = “Swift Attributed String” let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ] let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) // … Read more

Adjust UILabel height depending on the text

sizeWithFont constrainedToSize:lineBreakMode: is the method to use. An example of how to use it is below: //Calculate the expected size based on the font and linebreak mode of your label // FLT_MAX here simply means no constraint in height CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX); CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; //adjust the label the … Read more