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

后端 未结 21 1538
傲寒
傲寒 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:20

    Building on top of totiG's awesome answer, I have created an IBDesignable class that makes it extremely easy to customize a UILabel's vertical alignment right from the StoryBoard. Just make sure that you set your UILabel's class to 'VerticalAlignLabel' from the StoryBoard identity inspector. If the vertical alignment doesn't take effect, go to Editor->Refresh All Views which should do the trick.

    How it works: Once you set your UILabel's class correctly, the storyboard should show you an input field that takes an integer (alignment code).

    Update: I've added support for centered labels ~Sev


    Enter 0 for Top Alignment

    Enter 1 for Middle Alignment

    Enter 2 for Bottom Alignment

        @IBDesignable class VerticalAlignLabel: UILabel {
        
        @IBInspectable var alignmentCode: Int = 0 {
            didSet {
                applyAlignmentCode()
            }
        }
        
        func applyAlignmentCode() {
            switch alignmentCode {
            case 0:
                verticalAlignment = .top
            case 1:
                verticalAlignment = .topcenter
            case 2:
                verticalAlignment = .middle
            case 3:
                verticalAlignment = .bottom
            default:
                break
            }
        }
        
        override func awakeFromNib() {
            super.awakeFromNib()
            self.applyAlignmentCode()
        }
        
        override func prepareForInterfaceBuilder() {
            super.prepareForInterfaceBuilder()
            
            self.applyAlignmentCode()
        }
        
        enum VerticalAlignment {
            case top
            case topcenter
            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 #available(iOS 9.0, *) {
                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 .topcenter:
                        return CGRect(x: self.bounds.size.width - (rect.size.width / 2), 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 .topcenter:
                        return CGRect(x: (self.bounds.size.width / 2 ) - (rect.size.width / 2), 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)
                    }
                }
            } else {
                // Fallback on earlier versions
                return rect
            }
        }
        
        override public func drawText(in rect: CGRect) {
            let r = self.textRect(forBounds: rect, limitedToNumberOfLines: self.numberOfLines)
            super.drawText(in: r)
        }
    }

提交回复
热议问题