首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

iOS Swift 改变Label文字位置和Button图文位置

2024-12-10 来源:华佗小知识

改变Label文字位置

通过重写drawText方法,改变frame位置,后面延展方法是改变文字间的间距

import UIKit
enum VerticalAlignment {
    case VerticalAlignmentTop
    case VerticalAlignmentMiddle
    case VerticalAlignmentBottom
}
class VKYCustomLabel: UILabel {

    var verticalAlignment: VerticalAlignment {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override init(frame: CGRect) {
        verticalAlignment = .VerticalAlignmentMiddle
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func drawText(in rect: CGRect) {
        let actualRect = self.textRect(forBounds: rect, limitedToNumberOfLines: numberOfLines)
        super.drawText(in: actualRect)
    }
    
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        var textRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        switch self.verticalAlignment {
        case .VerticalAlignmentTop:
            textRect.origin.y = bounds.origin.y
        case .VerticalAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height
        case .VerticalAlignmentMiddle:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0
        }
        return textRect
    }
}

extension UILabel {
    open func changeWordSpace(space: CGFloat){
        guard let labelText = self.text else {
            return
        }
        let newAttributedText = NSMutableAttributedString.init(string: labelText)
        newAttributedText.addAttribute(NSKernAttributeName, value: space, range: NSMakeRange(0, labelText.length))
        self.attributedText = newAttributedText
        self.sizeToFit()
        
    }
    
    open func changeLineSpace(space: CGFloat){
        guard let labelText = self.text else {
            return
        }
        let newAttributedText = NSMutableAttributedString.init(string: labelText)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = space
        newAttributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, labelText.length))
        self.attributedText = newAttributedText
        self.sizeToFit()
    }
    
    open func changeSpace(wordSpace: CGFloat, lineSpace: CGFloat){
        guard let labelText = self.text else {
            return
        }
        let newAttributedText = NSMutableAttributedString.init(string: labelText)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        newAttributedText.addAttribute(NSKernAttributeName, value: wordSpace, range: NSMakeRange(0, labelText.length))
        newAttributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, labelText.length))
        self.attributedText = newAttributedText
        self.sizeToFit()
    }
}

改变UIButton图文位置

通过重写layoutSubViews方法,调整imageEdgeInsets和titleEdgeInsets来改变UIButton的按钮和图片位置

import UIKit
enum ImagePosition {
    case left  //default
    case right
    case top
    case bottom
}

class VKYCustomButton: UIButton {

    var imagePosition: ImagePosition {
        didSet{
            layoutSubviews()
        }
    }
    
    override init(frame: CGRect) {
        self.imagePosition = .left
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        guard let imageWidth = imageView?.image?.size.width,
        let imageHeight = imageView?.image?.size.height,
        let labelWidth =  titleLabel?.frame.width,
        let labelHeight = titleLabel?.frame.height else {
            return
        }
        switch imagePosition {
        case .left:
            return
        case .right:
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth)
            titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth)
        case .top:
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight/2, labelWidth/2, labelHeight/2, -labelWidth/2)
            titleEdgeInsets = UIEdgeInsetsMake(imageHeight/2, -imageWidth/2, -imageHeight/2, imageWidth/2)
        case .bottom:
            imageEdgeInsets = UIEdgeInsetsMake(labelHeight/2, -labelWidth/2, -labelHeight/2, labelWidth/2)
            titleEdgeInsets = UIEdgeInsetsMake(-imageHeight/2, imageWidth/2, imageHeight/2, -imageWidth/2)
            
        }
        
    }

}
显示全文