Set insets on UILabel - ios

I'm trying to set some insets in a UILabel. It worked perfectly, but now UIEdgeInsetsInsetRect has been replaced with CGRect.inset(by:) and I can't find out how to solve this.
When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.
My code
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: CGRect.insetBy(inset))
// super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
}
}
Anyone knows how to set the insets to the UILabel?

Imho you also have to update the intrinsicContentSize:
class InsetLabel: UILabel {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: inset))
}
override var intrinsicContentSize: CGSize {
var intrinsicContentSize = super.intrinsicContentSize
intrinsicContentSize.width += inset.left + inset.right
intrinsicContentSize.height += inset.top + inset.bottom
return intrinsicContentSize
}
}

Please update your code as below
class TagLabel: UILabel {
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.insetBy(inset))
}
}

this code differs from the accepted answer because the accepted answer uses insetBy(inset) and this answer uses inset(by: inset). When I added this answer in iOS 10.1 and Swift 4.2.1 autocomplete DID NOT give you rect.inset(by: ) and I had to manually type it in. Maybe it does in Swift 5, I'm not sure
For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)
this:
override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.drawText(in: rect.inset(by: inset))
}

Swift 5
replace method drawText(...) with draw(...)
extension UILabel {
open override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
super.draw(rect.inset(by: inset))
}}

Related

Add padding/insets to UIImage as background of UITextField

I want to have the triangle on the right side of the textfield with a little padding, currently it is right at the border:
My code:
class Textfield: UITextField, UITextFieldDelegate {
func setup() {
let background = UIImage(systemName: "arrowtriangle.down.fill")
let imageView = UIImageView(image: background)
imageView.tintColor = .label
self.rightViewMode = .always
imageView.contentMode = .scaleAspectFit
self.rightView = imageView
}
}
How can I add insets so the triangle is a little bit more left?
You can override the text fields methods. Like this
class Textfield: UITextField, UITextFieldDelegate {
#IBInspectable var leftPadding : CGFloat = 0
#IBInspectable var rightPadding : CGFloat = 10
private var padding: UIEdgeInsets = UIEdgeInsets()
override func awakeFromNib() {
super.awakeFromNib()
padding = UIEdgeInsets(top: 0, left: leftPadding, bottom: 0, right: rightPadding)
setup()
}
func setup() {
let background = UIImage(systemName: "arrowtriangle.down.fill")
let imageView = UIImageView(image: background)
imageView.tintColor = .label
self.rightViewMode = .always
imageView.contentMode = .scaleAspectFit
self.rightView = imageView
}
override open func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override open func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 30, y: 0, width: 20 , height: bounds.height)
}
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: 10, y: 0, width: 20 , height: bounds.height)
}
}

Subclass UINavigationBar SWIFT

I want to pose my problem with the hope that someone can help me:
I created a subclass of UINavigationBar, adding an image as a background.
here is the code:
class NavigationBarN: UINavigationBar {
let customHeight: CGFloat = UIScreen.main.bounds.height / 8
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
translatesAutoresizingMaskIntoConstraints = true
self.barStyle = UIBarStyle.black
self.tintColor = .white
}
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = true
self.barStyle = UIBarStyle.black
self.tintColor = .white
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: customHeight)
}
var i = 0
override open func layoutSubviews() {
super.layoutSubviews()
frame = CGRect(x: frame.origin.x, y: 0, width: frame.size.width, height: customHeight) // problem here
for subview in self.subviews {
var stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("BarBackground") {
subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: customHeight))
imageViewBackground.image = UIImage(named: “Image”1)
imageViewBackground.contentMode = .scaleToFill
subview.addSubview(imageViewBackground)
}
stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("BarContent") {
let centerY = subview.frame.heigh
subview.frame = CGRect(x: subview.frame.origin.x, y: centerY, width: subview.frame.width, height: subview.frame.height)
}
}
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
Up to iOS 11, it works without problems since iOS 12 the problems begin, the subclass enters an infinite loop. However the infinite loop occurs only when I try to initialize a new VC through the "navigationController? .PushViewController" function, otherwise it works.
The line of code causing the loop apparently is:
frame = CGRect(x: frame.origin.x, y: 0, width: frame.size.width, height: customHeight)
But removing it the navigation bar is not created correctly.
Anyone have any solution?
Thank You
You shouldn't change your frame in layoutSubviews – you should only be laying out your subviews. Changing the size of the frame in layoutSubviews couples the layout and sizing of the view, which you shouldn't do.
It's likely that under the hood, UINavigationBar is calling setNeedsLayout when its frame gets set.

how to give padding to a label text in collectionviewcell in swift?

I have UICollectionViewCell and inside cell their is one label without set width constraints that text automatically set ,but I need left and right spacing from the text in the label in swift
You should have custom UILabel class and in that class override drawTextInRect :
override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
If your label has more than one line it's better to use UITextView and change textContainerInset property:
let textView = UITextView()
textView.textContainerInset = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)

How can I modify my UILabel subclass so that it can accept custom values?

This is my current class.
class PaddedUILabel: UILabel {
var padding = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
// Override -intrinsicContentSize: for Auto layout code
override var intrinsicContentSize : CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let heigth = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
// Override -sizeThatFits: for Springs & Struts code
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits = super.sizeThatFits(size)
let width = superSizeThatFits.width + padding.left + padding.right
let heigth = superSizeThatFits.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
}
Right now, the paddings are hardcoded. I'd like to be able to set custom values in StoryBoard UI (top, left, bottom, right).
What can I do to my code to modify this?
import Foundation
#IBDesignable class UILabelExtendedView: UILabel {
#IBInspectable var topInset: CGFloat = 4.0
#IBInspectable var bottomInset: CGFloat = 4.0
#IBInspectable var leftInset: CGFloat = 8.0
#IBInspectable var rightInset: CGFloat = 8.0
override func drawText(in rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override public var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
func setPadding(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
self.topInset = top
self.bottomInset = bottom
self.leftInset = left
self.rightInset = right
let insets: UIEdgeInsets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
super.drawText(in: UIEdgeInsetsInsetRect(self.frame, insets))
}}
Pass this class name to your label in storyboard and in attribute inspector set your padding.

UILabel border and padding

I want to add padding inside my label in order to have spaces between it and its border. I created for that a class that extends from UILabel.
UILabelPadding.swift:
import UIKit
class UILabelPadding: UILabel {
let padding = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
override var intrinsicContentSize : CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let heigth = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits = super.sizeThatFits(size)
let width = superSizeThatFits.width + padding.left + padding.right
let heigth = superSizeThatFits.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
}
and I changed the type of myLabel from UILabel to UILabelPadding. In my UIViewController, I set the text of myLabel, call the sizeToFit() and after that I add the border and the background color to myLabel:
myLabel.text = "label test"
myLabel.sizeToFit()
//background + border
myLabel.layer.borderColor = UIColor(red: 27/255, green: 100/255, blue: 90/255, alpha: 1.0).cgColor
myLabel.layer.backgroundColor = UIColor(red: 27/255, green: 100/255, blue: 90/255, alpha: 1.0).cgColor
myLabel.layer.cornerRadius = 9
myLabel.layer.masksToBounds = true
myLabel.layer.borderWidth = 1
The border and the background color are added but the padding is not working. When I debug, the sizeThatFits() is never called.
Any help please?
I solved the issue!
For those how are facing the same problem:
1- Make a class extending from UILabel:
UILabelPadding.swift:
class UILabelPadding: UILabel {
let padding = UIEdgeInsets(top: 2, left: 8, bottom: 2, right: 8)
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: padding))
}
override var intrinsicContentSize : CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let heigth = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
}
2- Set the type of your label to the UILabelPadding and make sure that the type is set also in the storyboard.
I use a label subclass that does much what you describe. It looks like this:
class MyBoundedLabel: UILabel {
override func drawText(in rect: CGRect) {
let context = UIGraphicsGetCurrentContext()!
context.stroke(self.bounds.insetBy(dx: 1.0, dy: 1.0))
super.drawText(in: rect.insetBy(dx: 5.0, dy: 5.0))
}
}
You might try that, and then jiggle the numbers and other details so they look like what you want.
I have used this solution in my app. Please check below codes.
class CustomLabel: UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let textRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top,
left: -textInsets.left,
bottom: -textInsets.bottom,
right: -textInsets.right)
return textRect.inset(by: invertedInsets)
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: textInsets))
}
}

Resources