swift - 当我添加 imageView 时,UILabel 文本以错误的对齐方式截断

标签 swift uiimageview uiimage uilabel subclass

我有一个子类 UILabel,我将在 IB 中将其用于大约 10 个不同长度的不同标签(全部在 1 行!)。每个标签中的文本都是静态的。

这个子类化的 UILabel 应该在文本的左边显示一个 imageView。这个 imageView 将包含一个小图标。

它快要工作了,除了从下面的屏幕截图中可以看到奇怪的截断和对齐。

模拟器:

Text truncating

Storyboard设置:

Storyboard Setup

这是我的代码:

class ImageMarkLabel: UILabel {

    var labelHeight:CGFloat = 0.0
    let smileView = UIView()
    let smileImageView = UIImageView()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.commonInit()

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        uiStuff()
    }

    func uiStuff() {
        labelHeight = self.frame.height
        smileView.frame = CGRect(x: 0, y: 0, width: labelHeight, height: labelHeight)
        smileImageView.image = UIImage(named: "smile")
        smileImageView.frame = CGRect(x: 0, y: 0, width: smileView.frame.width, height: smileView.frame.height)
    }

    func commonInit(){
        smileView.addSubview(smileImageView)
        self.addSubview(smileView)
    }

    override func drawText(in rect: CGRect) {
        var insets = UIEdgeInsets()
        if UIDevice.current.userInterfaceIdiom == .pad {
            insets = UIEdgeInsets(top: 0, left: labelHeight + 10.0, bottom: 0, right: labelHeight + 10.0)
        }
        else {
            insets = UIEdgeInsets(top: 0, left: labelHeight + 5.0, bottom: 0, right: labelHeight + 5.0)
        }
        super.drawText(in: rect.inset(by: insets))
    }

}

关于自动布局 - 所有标签都在垂直堆栈 View 中。

如何让文本和 imageView 居中而不导致任何文本截断?

最佳答案

UIStuff 方法中改变你的框架,

class ImageMarkLabel: UILabel {

    var labelHeight:CGFloat = 0.0
    let smileView = UIView()
    let smileImageView = UIImageView()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.commonInit()

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        uiStuff()
    }

    func uiStuff() {
        labelHeight = self.frame.height
        smileView.frame = CGRect(x: 0, y: 0, width: labelHeight, height: labelHeight)
        smileImageView.image = UIImage(named: "smile")
        smileImageView.frame = CGRect(x: 0, y: 0, width: smileView.frame.width, height: smileView.frame.height)

        var insets = UIEdgeInsets()
        if UIDevice.current.userInterfaceIdiom == .pad {
            insets = UIEdgeInsets(top: 0, left: (labelHeight + 10.0), bottom: 0, right: (labelHeight + 10.0))
        }
        else {
            insets = UIEdgeInsets(top: 0, left: (labelHeight + 5.0), bottom: 0, right: (labelHeight + 5.0))
        }
        let newRect = CGRect(origin: self.frame.origin, size: CGSize(width: self.frame.width + 2*insets.left, height: self.frame.height))
        self.frame = newRect
    }

    func commonInit(){
        smileView.addSubview(smileImageView)
        self.addSubview(smileView)

    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: rect)
    }

}

输出:

enter image description here

关于swift - 当我添加 imageView 时,UILabel 文本以错误的对齐方式截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738769/

相关文章:

ios - 如何将信息从一个 View Controller 中的 TableView 单元格传递到另一个 View Controller ?

swift - 转换 [字符串 : String] to [String : URL] and flattening out nil values

ios - 图像在约束条件下未按预期工作

iphone - 将新图像加载到 UIImageView 中,之前图像的内存不会被释放

ios - 从函数中的数组返回一个项目

iphone - 像 camscanner 一样裁剪 UIImage

arrays - 在 Swift 数组中执行数学函数

ios - UIImageView 多个图像之间的过渡

ios - 在 NSArray 中使用 UIImageViews 填充 UICollectionView 单元格

swift - CGFloat 和 NSNumber 之间的转换,无需不必要地提升为 Double