How to Add Kerning to a Textfield in Swiftui

SwiftUI ViewModifier - add kerning

Alternate is to use Text-only modifier, like

extension Text {   
func applyLabelFont(size: CGFloat, kerning: CGFloat = 4) -> Text {
self
.font(.custom(Constants.defaultLabelFontSFProDisplayThin, size: size))
.kerning(kerning)
}
}

Can't seem to utilize Spacer() between text() and textfield()

try adding .fixedSize() modifier to your TextField

   TextField("Enter URL", text: $url)
.fixedSize()

or set a frame like so

   TextField("Enter URL", text: $url)
.frame(width:200, height:50, alignment:.leading)

The problem is that TextField and Spacer() would take all available space and in this case TextField gets the priority; However, if you specify a fixed size or a frame to it then TextField won't stretch to take full space instead it will be fixed.

.fixedSize would allow your TextField to start small but eventually it will keep stretching the more text you write which can cause unwanted behavior.

.frame will fix your size to the provided width and hence there won't be any stretch and Spacer will have priority to take available space.

How to set letter spacing of UITextField

No need to go for attributedText, which to be honest, was a mess implementing with modified spacing. As soon as I closed the keyboard the spacing disappeared, which prompted me to dig further.

Every UITextField has a property called defaultTextAttributes, which according to Apple "returns a dictionary of text attributes with default values.". The Apple document also says that "this property applies the specified attributes to the entire text of the text field"

Just find a suitable place in your code, usually where the textfield is being initialized and then copy and paste the following.

Answered in Swift 3.0

textfield.defaultTextAttributes.updateValue(spacing, forKey: NSKernAttributeName)

where spacing is of CGFloat type. For example 2.0

This works for different fonts as well.

Cheers!!


The latest syntax seems to be:

 yourField.defaultTextAttributes.updateValue(36.0, 
forKey: NSAttributedString.Key.kern)

Add label to SwiftUI TextField

I was able to get this to work by wrapping it in an HStack

HStack {
Text("Name")

Spacer()

TextField("", text: $name).multilineTextAlignment(.trailing)
}

Is there a way to set font tracking (i.e. spacing) inside a custom SwiftUI ButtonStyle?

Yes, ButtonStyleConfiguration.Label is not matched to Text, but it is your style you can ignore standard label, and request own by interface contract, exactly which you need (Text in this case), like in below demo

public struct MyStyle: ButtonStyle {
let label: Text // << here !!

public func makeBody(configuration: Configuration) -> some View {
label // << here !!
.tracking(10)
.foregroundColor(configuration.isPressed ? .black : .red)
.clipShape(Capsule())
}
}

and use it as (or create own button extension with button builder to hide those unneeded curls)

Button(action: {
// action here
}){}
.buttonStyle(MyStyle(label: Text("Hello")))

Tested with Xcode 13.2 / iOS 15.2

Sample Image

How do you create textfield padding in Swift 4?

As @the4kman says, Swift does support CGRect but the syntax may have changed.

You can try this for instance:

@IBOutlet weak var nameTextField: UITextField! {
didSet {
nameTextField.layer.cornerRadius = 5
nameTextField.layer.borderColor = UIColor.black.cgColor
nameTextField.layer.borderWidth = 1
let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 2.0))
nameTextField.leftView = leftView
nameTextField.leftViewMode = .always
}
}

If I do that, I get this fine result

left padding

Hope that helps.

Update

You ask for a function instead of setting it in didSet and sure, thats possible, something like:

func addPaddingAndBorder(to textfield: UITextField) {
textfield.layer.cornerRadius = 5
textfield.layer.borderColor = UIColor.black.cgColor
textfield.layer.borderWidth = 1
let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 2.0))
textfield.leftView = leftView
textfield.leftViewMode = .always
}

and then you' call that in viewDidLoad for instance like so:

override func viewDidLoad() {
super.viewDidLoad()
addPaddingAndBorder(to: nameTextField)
}

Change character spacing on UILabel within Interface Builder

Ended up using this for now to get existing attributed text and modify to add character spacing:

let attributedString = discoveryTitle.attributedText as NSMutableAttributedString
attributedString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, attributedString.length))
discoveryTitle.attributedText = attributedString

Swift 3:

let attributedString = NSMutableAttributedString(string: discoveryTitle.text)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
discoveryTitle.attributedText = attributedString

Using NSRange instead of NSMakeRange works in Swift 3.



Related Topics



Leave a reply



Submit