Thursday, November 9, 2023
HomeiOS Developmentios - Textual content on SwiftUI getting lower off on trailing aspect,...

ios – Textual content on SwiftUI getting lower off on trailing aspect, any options?


I am having an issue with customized Font in SwiftUI, I am use Google Fonts Allura,Courgette,Mr Dafoe, Shoadows Into Mild Two, and Supply Sans Professional. I supplied picture beneath of instance you possibly can its chopping the character both from the again or the entrance and solely doing them to characters with top, like D,H,J,I and and so on.

enter picture description right here
enter picture description right here

I’ve tried giving it a body with .infinity and adjusting the peak. I’ve additionally tried including padding and padding(trailing). Moreover, I’ve tried utilizing .fixedSize(horizontal: true, vertical: false) and .scaledToFit(), however none of those strategies have labored. I suspected there is perhaps a problem with my view, so I created a completely new challenge in SwiftUI, gave Textual content(“Whats up World!”) a customized font, and encountered the identical difficulty.

Then, whereas looking out, I discovered a customized UIKit label, which labored after I tried it. Plainly when the customized font is utilized as a UIFont, it really works completely, however when it is used as a SwiftUI.Font, it doesn’t.

I’ve even tried integrating the customized UIKit label into my code, however the issue persists. I would like the label to resize and alter the font measurement dynamically because the variety of characters will increase, to suit on one line and inside the view’s width. Under is the SwiftUI code because the Minimal Deployment is iOS 15 only for aheads up.


                HStack(spacing: 8) {
                    Spacer()
                    Textual content(signText)
                        .lineLimit(1)
                        .allowsTightening(true)
                        .minimumScaleFactor(0.5 / 2)
                        .font(font)

                        .body(maxWidth: .infinity,idealHeight: 150, alignment: .heart)
                    
                    Spacer()
                    if !signText.isEmpty {
                        Button(motion: {
                            isSignSheetPresented = true
                        }, label: {
                            Picture("edit")
                                .renderingMode(.template)
                                .body(idealWidth: 44)
                                .foregroundColor(Shade(CanopyColor.major.colour))
                        })
                    }
                }
                .padding(16)
                .body(maxWidth: .infinity, idealHeight: 150, alignment: .heart )
                .background(Shade(crimson: 0.98, inexperienced: 0.98, blue: 0.98)) // Shade Change
                .cornerRadius(4)
                .overlay(
                  RoundedRectangle(cornerRadius: 4)
                    .inset(by: 0.5)
                    .stroke(Shade(crimson: 0.87, inexperienced: 0.87, blue: 0.87), lineWidth: 1) 
                )

That is the UILabel that I used

struct UILabelView: UIViewRepresentable {
    var textual content: String
    var font: UIFont
    
    func makeUIView(context: Context) -> UILabel {
        let label = UILabel()
        label.font = font
        label.textual content = textual content
        label.numberOfLines = 1
        label.textAlignment = .heart
        label.backgroundColor = .clear
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.25
        label.allowsDefaultTighteningForTruncation = true
        label.translatesAutoresizingMaskIntoConstraints = false
        label.widthAnchor.constraint(equalToConstant: 100).isActive = true
        return label
    }
    
    func updateUIView(_ uiView: UILabel, context: Context) {
        uiView.textual content = textual content
        uiView.font = font
        uiView.textAlignment = .heart
    }
}

If there anymore query or info that I’ve missed please Remark so I can add them.

Additionally all of the font I discussed above are from Google Fonts listed below are the title of them in code so it simpler to search out.

Allura-Common Courgette-Common MrDafoe-Common ShadowsIntoLightTwo-Common SourceSansPro-Common



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments