Skip to content

Label text goes out of bounds #227

Open
@Shishani58

Description

@Shishani58

Thank you for such an incredible library.
My issue:
In some cases, the text (it's arabic text) goes out of bounds and I can't understand why.
I tried calling sizeToFit, but it didn't help.
I attach a screenshot and a code
Screenshot 2021-04-17 at 04 47 00

import UIKit
import PinLayout

final class AyahCell: UITableViewCell {

    //MARK: Static properties
    /// Верхний отступ для arabicLabel (12)
    static let topMargin: CGFloat = 12
    /// Нижний отступ для arabicLabel (3)
    static let bottomMargin: CGFloat = 3
    /// Левый отступ для numberView (8)
    static let leftMargin: CGFloat = 8
    /// Правый отступ для arabicLabel (12)
    static let rightMargin: CGFloat = 8
    /// Левый отступ для arabicLabel (5)
    static let arabicLabelLeftMargin: CGFloat = 5
    /// Верхний отступ для numberView (5)
    static let numberViewTopMargin: CGFloat = 5
    /// Высота для numberView (35)
    static let numberViewWidth: CGFloat = 35
    /// Максимальная ширина arabicLabel
    static let maxWidth = UIScreen.main.bounds.width - AyahCell.leftMargin - AyahCell.rightMargin - AyahCell.numberViewWidth - AyahCell.arabicLabelLeftMargin
    
    //MARK: - Views
    private let separatorView = UIView()
    private let numberView = UIView()
    private let numberImageView = UIImageView(image: AyahCellResources.numberImage)
    private let arabicLabel: UILabel = {
        let label = UILabel(textAlignment: .right, numberOfLines: 0)
        return label
    }()
    private let numberLabel: UILabel = {
        let label = UILabel(textAlignment: .center, font: UIFont(name: "Georgia", size: 14), numberOfLines: 1)
        return label
    }()
    
    //MARK: - Properties
    var cellModel: AyahCellModel!
    
    //MARK: Init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none

        contentView.addSubview(separatorView)
        separatorView.pin.height(1)

        contentView.addSubview(numberView)
        numberView.addSubview(numberLabel)
        numberView.addSubview(numberImageView)

        contentView.addSubview(arabicLabel)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        layout()
    }
    
    private func layout() {
        separatorView.pin.top().left().right()

        numberView.pin
            .below(of: separatorView)
            .marginTop(AyahCell.numberViewTopMargin)
            .left(AyahCell.leftMargin)
            .size(AyahCell.numberViewWidth)

        numberImageView.pin.all()
        numberLabel.pin.all()

        arabicLabel
            .pin
            .after(of: numberView)
            .marginLeft(AyahCell.arabicLabelLeftMargin)
            .top(AyahCell.topMargin)
            .right(AyahCell.rightMargin)
            .bottom(AyahCell.bottomMargin)
    }

}

//MARK: Setup
extension AyahCell {
    
    func configure(with model: AyahCellModel) {
        self.cellModel = model
        let number = "\(self.cellModel.ayahNumber)"
        self.arabicLabel.attributedText = self.cellModel.text
        self.numberLabel.text = number
    }
    
}

//MARK: Cell Size
extension AyahCell {
    
    /// Атрибуты для арабского текста
    static var arabicTextAtrributes: [NSAttributedString.Key : Any] {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 12
        paragraphStyle.alignment = .right
        paragraphStyle.lineBreakMode = .byWordWrapping
        
        let attributes: [NSAttributedString.Key : Any] = [
            .foregroundColor : Theme.current.textColor,
            .font: ArabicFont.current.quranTextFont,
            .paragraphStyle : paragraphStyle]
        return attributes
    }
    
    /// Расчет высоты для ячейки с текстом Корана
    static func calculateCellHeight(text: NSMutableAttributedString) -> CGFloat {
        let screenWidth = UIScreen.main.bounds.width
        let maxWidth: CGFloat =
            screenWidth - AyahCell.numberViewWidth - AyahCell.arabicLabelLeftMargin - AyahCell.leftMargin - AyahCell.rightMargin
        let height = (
            text.size(maxWidth: maxWidth).height + AyahCell.topMargin + AyahCell.bottomMargin).rounded(.up)
        return ceil(height)
    }
    
}

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