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

Vertical align not correct after setting textAligment #24

Closed
polqf opened this issue Dec 5, 2015 · 7 comments · Fixed by #35
Closed

Vertical align not correct after setting textAligment #24

polqf opened this issue Dec 5, 2015 · 7 comments · Fixed by #35

Comments

@polqf
Copy link
Contributor

polqf commented Dec 5, 2015

Hi,

I am using ActiveLabel in my app, and there's a case where I need the text to be horizontal and vertical aligned. But setting the textAlignment property does not change anything.

I added a line on the addLineBreak(attrString:) method expecting it to center the text as expected, but the result is the following:

screen shot 2015-12-05 at 19 32 29

Correct center alignment, but incorrect vertical one.

/// add line break mode
    private func addLineBreak(attrString: NSAttributedString) -> NSMutableAttributedString {
        let mutAttrString = NSMutableAttributedString(attributedString: attrString)

        var range = NSRange(location: 0, length: 0)
        var attributes = mutAttrString.attributesAtIndex(0, effectiveRange: &range)

        let paragraphStyle = attributes[NSParagraphStyleAttributeName] as? NSMutableParagraphStyle ?? NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
        paragraphStyle.alignment = textAlignment //ADDED THIS
        if let lineSpacing = lineSpacing {
            paragraphStyle.lineSpacing = CGFloat(lineSpacing)
        }

        attributes[NSParagraphStyleAttributeName] = paragraphStyle
        mutAttrString.setAttributes(attributes, range: range)

        return mutAttrString
    }

A solution to accomplish the center alignment would be to add a didSet on it, as on the other variables.
As for the vertical alignment, I tried a lot of things to accomplish it, but without luck, so I have no clues...

PS. I have an extension of ActiveLabel that uses a delegate instead of having to set block for each Label you create to handle user touches. Do you want me to create a PR with it?

@schickling
Copy link
Contributor

Hi @poolqf. Thanks for your issue. Please go ahead and open a PR 👍

@ghost
Copy link

ghost commented Dec 18, 2015

Did you manage to fix this issue about the alignment?

@polqf
Copy link
Contributor Author

polqf commented Dec 19, 2015

Nope @xScylez , the only thing I fixed was horizontal alignment, but not the vertical one.

But a workaround could be to use the horizontal alignment fix, and then make the UILabel height fit to size, and then manually center the label inside the view you want

@ghost
Copy link

ghost commented Dec 19, 2015

@poolqf Could you post an example code for your workaround please? I don't fully understand what you mean.

@polqf
Copy link
Contributor Author

polqf commented Dec 19, 2015

@xScylez taking #31 , that is fixing the horizontal alignment, the workaround for fixing de vertical alignment, in a really verbose way, could be something like this:

    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: 150, height: 50)
    let label = ActiveLabel()
    label.text = "My Text"
    label.textAlignment = .Center
    label.sizeToFit()
    let containerHeight = containerView.frame.height
    label.frame = CGRect(x: 0, y: containerHeight/2 - label.frame.height/2, width: containerView.frame.width, height: label.frame.height)
    containerView.addSubview(label)

It is just vertically centering the label inside the container view

@ghost
Copy link

ghost commented Dec 20, 2015

@poolqf What about if the label is already declared as a ActiveLabel in Storyboard? I can access the label using the command imageCell.imageText.

@polqf
Copy link
Contributor Author

polqf commented Dec 20, 2015

@xScylez using Storyboards it should be even easier if you play with the correct constraints

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

Successfully merging a pull request may close this issue.

2 participants