Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid line spacing for last line with button #9

Closed
ivanvorobei opened this issue Jul 3, 2020 · 25 comments
Closed

Invalid line spacing for last line with button #9

ivanvorobei opened this issue Jul 3, 2020 · 25 comments

Comments

@ivanvorobei
Copy link

ivanvorobei commented Jul 3, 2020

Attached example:

Code for init:

let text = AttributedString(NSAttributedString(string: text), .font(textFont), .color(descriptionLabel.textColor))
        let button = AttributedString(NSAttributedString(string: button), .font(buttonFont), .color(buttonColor), .action([.color(buttonColor.withAlphaComponent(0.5))], action))
        descriptionLabel.attributed.text = text + " " + button

Code for layout

titleLabel.frame.origin = .zero
titleLabel.frame.set(width: frame.width)
titleLabel.sizeToFit()
        
descriptionLabel.frame = CGRect.init(x: 0, y: titleLabel.frame.bottomY + 4, width: frame.width, height: 0)
descriptionLabel.sizeToFit()

Number of lines set to zero.

@lixiang1994
Copy link
Owner

lixiang1994 commented Jul 4, 2020

I found that when there are two different font sizes in a row, it will look like in the screenshot.

This is a problem of NSAttributedString, but I know how to achieve the desired effect.

Can be solved by setting the line height:

label.attributed.text = .init(text + " " + button, .paragraph(.maximumLineHeight(30), .minimumLineHeight(30)))

e.g.

image

image

@lixiang1994
Copy link
Owner

The root cause is the difference between line spacing and line height.

@ivanvorobei
Copy link
Author

I try set -2, and it no do anything for me. If positive - doing

@lixiang1994
Copy link
Owner

Is the line height invalid?

@ivanvorobei
Copy link
Author

I set only maximum & minimum, no do any additional. It need?

@lixiang1994
Copy link
Owner

let text = AttributedString(string: text, .font(textFont), .color(descriptionLabel.textColor))
        let button = AttributedString(string: button, .font(buttonFont), .color(buttonColor), .action([.color(buttonColor.withAlphaComponent(0.5))], action))
descriptionLabel.attributed.text = .init(text + " " + button, .paragraph(.maximumLineHeight(30), .minimumLineHeight(30)))

After running this code, what is the result?

@ivanvorobei
Copy link
Author

ivanvorobei commented Jul 7, 2020

With 30 spacing more. But I try set -2 or even zero. Result this:

Screenshot 2020-07-07 at 13 52 40

Last line with button have bigger space

@lixiang1994
Copy link
Owner

God, can you extract more complete code for me, I need to debug and analyze.

@lixiang1994
Copy link
Owner

Before that, I need to confirm whether the system font is used?

@ivanvorobei
Copy link
Author

ivanvorobei commented Jul 7, 2020

Sorry, I simple use your code as you send.
This that I use

let textFont = UIFont.preferredFont(forTextStyle: .footnote)
let buttonFont = UIFont.preferredFont(forTextStyle: .footnote, weight: .medium)
        
let textAttributed = AttributedString(NSAttributedString(string: text + text), .font(textFont), .foreground(descriptionLabel.textColor))
let buttonAttributed = AttributedString(NSAttributedString(string: button), .font(buttonFont), .foreground(buttonColor), .action([.foreground(buttonColor.withAlphaComponent(0.5))], action))
descriptionLabel.attributed.text = .init(textAttributed + " " + buttonAttributed, .paragraph(.maximumLineHeight(-2), .minimumLineHeight(-2)))

@lixiang1994
Copy link
Owner

UIFont.preferredFont(forTextStyle: .footnote, weight: .medium) ??? Is it an extension?

@lixiang1994
Copy link
Owner

my code:

image

image

@lixiang1994
Copy link
Owner

Line height must be greater than 0 to take effect

@ivanvorobei
Copy link
Author

@lixiang1994 you full right. I set zero and it not work. When I set textFont.lineHeight it start work
Thanks for your time and big help!

@lixiang1994
Copy link
Owner

Line spacing and line height are different.

@lixiang1994
Copy link
Owner

My pleasure

@ivanvorobei
Copy link
Author

Sorry, catch strange behaviour.

Code

let textFont = UIFont.preferredFont(forTextStyle: .footnote)
let buttonFont = UIFont.preferredFont(forTextStyle: .footnote, weight: .medium).rounded()
let buttonColor = SPInterface.tintColor
        
let text = "you full right. I set zero and it not work. When I set textFont.lineHeight it start work. Thanks for your time and big help!"
let button = "Action"
let textAttributed = AttributedString(NSAttributedString(string: text), .font(textFont), .foreground(descriptionLabel.textColor))
        let buttonAttributed = AttributedString(NSAttributedString(string: button), .font(buttonFont), .foreground(buttonColor), .action([.foreground(buttonColor.withAlphaComponent(0.5))], {
            print("call")
        }))
descriptionLabel.numberOfLines = 0
descriptionLabel.attributed.text = .init(textAttributed + " " + buttonAttributed, .paragraph(.maximumLineHeight(textFont.lineHeight), .minimumLineHeight(textFont.lineHeight)))
        
scrollView.addSubview(descriptionLabel)
descriptionLabel.frame = CGRect.init(x: 20, y: 20, width: 200, height: 0)
descriptionLabel.sizeToFit()

It basic code, only with line height.
But if text small (you can set one work and button and text in 1 line) - all work, if many lines (like example which I attach), button not clickable

@ivanvorobei ivanvorobei reopened this Jul 7, 2020
@lixiang1994
Copy link
Owner

Try it.

scrollView.delaysContentTouches = fasle

@ivanvorobei
Copy link
Author

descriptionLabel placed not on scroll view

@lixiang1994
Copy link
Owner

???
image

@ivanvorobei
Copy link
Author

Yes, in example. I try remove it and place to view. Similar bug

@lixiang1994
Copy link
Owner

My code
image

Can you check the touch response?
For example Parent view isUserInteractionEnabled

@ivanvorobei
Copy link
Author

Even adding to empty project, also not work. I did example, you can check?
attributed-not-call.zip

@lixiang1994
Copy link
Owner

Thanks.
I have fixed it. 🎉
Because I use font leading, but some fonts are incomplete.
Please update to version 1.5.2.

@ivanvorobei
Copy link
Author

It work now!
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants