How to set top-left alignment for UILabel for iOS application?

后端 未结 21 1486
傲寒
傲寒 2020-12-04 07:54

I have added one label in my nib file, then its required to have top-left alignment for that lable. As I am providing text at runtime so its not sure that how much lines the

21条回答
  •  不思量自难忘°
    2020-12-04 08:27

    The SOLabel works for me.

    Swift 3 & 5:

    This version has been updated from the original to allow support for RTL languages:

    public class VerticalAlignLabel: UILabel {
        enum VerticalAlignment {
            case top
            case middle
            case bottom
        }
    
        var verticalAlignment : VerticalAlignment = .top {
            didSet {
                setNeedsDisplay()
            }
        }
    
        override public func textRect(forBounds bounds: CGRect, limitedToNumberOfLines: Int) -> CGRect {
            let rect = super.textRect(forBounds: bounds, limitedToNumberOfLines: limitedToNumberOfLines)
    
            if UIView.userInterfaceLayoutDirection(for: .unspecified) == .rightToLeft {
                switch verticalAlignment {
                case .top:
                    return CGRect(x: self.bounds.size.width - rect.size.width, y: bounds.origin.y, width: rect.size.width, height: rect.size.height)
                case .middle:
                    return CGRect(x: self.bounds.size.width - rect.size.width, y: bounds.origin.y + (bounds.size.height - rect.size.height) / 2, width: rect.size.width, height: rect.size.height)
                case .bottom:
                    return CGRect(x: self.bounds.size.width - rect.size.width, y: bounds.origin.y + (bounds.size.height - rect.size.height), width: rect.size.width, height: rect.size.height)
                }
            } else {
                switch verticalAlignment {
                case .top:
                    return CGRect(x: bounds.origin.x, y: bounds.origin.y, width: rect.size.width, height: rect.size.height)
                case .middle:
                    return CGRect(x: bounds.origin.x, y: bounds.origin.y + (bounds.size.height - rect.size.height) / 2, width: rect.size.width, height: rect.size.height)
                case .bottom:
                    return CGRect(x: bounds.origin.x, y: bounds.origin.y + (bounds.size.height - rect.size.height), width: rect.size.width, height: rect.size.height)
                }
            }
        }
    
        override public func drawText(in rect: CGRect) {
            let r = self.textRect(forBounds: rect, limitedToNumberOfLines: self.numberOfLines)
            super.drawText(in: r)
        }
    }
    

    Swift 1:

    class UIVerticalAlignLabel: UILabel {
    
    enum VerticalAlignment : Int {
        case VerticalAlignmentTop = 0
        case VerticalAlignmentMiddle = 1
        case VerticalAlignmentBottom = 2
    }
    
    var verticalAlignment : VerticalAlignment = .VerticalAlignmentTop {
        didSet {
            setNeedsDisplay()
        }
    }
    
    required init(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }
    
    override func textRectForBounds(bounds: CGRect, limitedToNumberOfLines: Int) -> CGRect {
        let rect = super.textRectForBounds(bounds, limitedToNumberOfLines: limitedToNumberOfLines)
    
        switch(verticalAlignment) {
            case .VerticalAlignmentTop:
                return CGRectMake(bounds.origin.x, bounds.origin.y, rect.size.width, rect.size.height)
            case .VerticalAlignmentMiddle:
                return CGRectMake(bounds.origin.x, bounds.origin.y + (bounds.size.height - rect.size.height) / 2, rect.size.width, rect.size.height)
            case .VerticalAlignmentBottom:
                return CGRectMake(bounds.origin.x, bounds.origin.y + (bounds.size.height - rect.size.height), rect.size.width, rect.size.height)
            default:
                return bounds
        }
    }
    
    override func drawTextInRect(rect: CGRect) {
        let r = self.textRectForBounds(rect, limitedToNumberOfLines: self.numberOfLines)
        super.drawTextInRect(r)
        }
    }
    

提交回复
热议问题