Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.
This repository was archived by the owner on Sep 23, 2026. It is now read-only.

textAlignment property is not working #84

Open

Description

@auro-krishna

textAlignment is not updating always aligns to left, How to fix this?

Activity

  1. mdeora commented on Nov 15, 2020

    @mdeora

    did you find a fix for this?

  2. auro-krishna commented on Nov 17, 2020

    @auro-krishna
    Author

    @mdeora Not yet

  3. Omeesh commented on Jan 28, 2021

    @Omeesh

    I fixed the issue by changing some code in Library

    1. Open the "ExpandableLabel.swift" file.
    2. Initialise a variable in the class. i.e
      open var customTextAlignment: NSTextAlignment?
    3. Replace copyWithParagraphAttribute function with following function
    func copyWithParagraphAttribute(_ font: UIFont,_ alignment: NSTextAlignment) -> NSAttributedString {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineHeightMultiple = 1.05
            paragraphStyle.alignment = alignment
            paragraphStyle.lineSpacing = 0.0
            paragraphStyle.minimumLineHeight = font.lineHeight
            paragraphStyle.maximumLineHeight = font.lineHeight
    
            let copy = NSMutableAttributedString(attributedString: self)
            let range = NSRange(location: 0, length: copy.length)
            copy.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
            copy.addAttribute(.baselineOffset, value: font.pointSize * 0.08, range: range)
            return copy
        }
    
    1. Replace the attributedText variable with following code
    open override var attributedText: NSAttributedString? {
            set(attributedText) {
                if let attributedText = attributedText?.copyWithAddedFontAttribute(font).copyWithParagraphAttribute(font, self. customTextAlignment ?? .left),
                    attributedText.length > 0 {
                    self.collapsedText = getCollapsedText(for: attributedText, link: (linkHighlighted) ? collapsedAttributedLink.copyWithHighlightedColor() : self.collapsedAttributedLink)
                    self.expandedText = getExpandedText(for: attributedText, link: (linkHighlighted) ? expandedAttributedLink?.copyWithHighlightedColor() : self.expandedAttributedLink)
                    super.attributedText = (self.collapsed) ? self.collapsedText : self.expandedText
                } else {
                    self.expandedText = nil
                    self.collapsedText = nil
                    super.attributedText = nil
                }
            }
            get {
                return super.attributedText
            }
        }
    
    1. Clean the build folder
    2. Set the alignment of label in your class
      label.customTextAlignment = .center
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions