Swift-自定义单元格内容(UILabel,UIImageView)

问题描述 投票:1回答:2

我想自定义我的UICollectionViewCell,但是我不希望它如何工作。

这是我第一个单元格的代码:

class MainWishlistCell: UICollectionViewCell {
    let wishlistImage: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.image = UIImage(named: "logoGroß")
        v.layer.cornerRadius = 3.0
        v.layer.shadowColor = UIColor.black.cgColor
        v.layer.shadowOffset = CGSize(width: 3, height: 3)
        v.layer.masksToBounds = false
        return v
    }()

    let wishlistLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.text = "Main Wishlist"
        v.font = UIFont(name: "Avenir Next-Bold", size: 18)
        v.textColor = .darkGray
        v.textAlignment = .center
        return v
    }()

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

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() -> Void {
        contentView.addSubview(wishlistImage)
        contentView.addSubview(wishlistLabel)
        // constrain view to all 4 sides
        NSLayoutConstraint.activate([
            wishlistImage.topAnchor.constraint(equalTo: contentView.topAnchor),
            wishlistImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            wishlistImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            wishlistImage.heightAnchor.constraint(equalToConstant:150),

            wishlistLabel.topAnchor.constraint(equalTo: wishlistImage.bottomAnchor,constant: 1),
            wishlistLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            wishlistLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            wishlistLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        ])
    }
}

它看起来像这样:

enter image description here

1。问题-为什么标签不是bold

2。问题-为什么没有cornerRadius

3。问题-为什么shadow不出现(底部+右侧)?

ios swift uiimage uilabel uicollectionviewcell
2个回答
1
投票

尝试一下

  1. 问题:1

[v.font = UIFont(name: "AvenirNext-Bold", size: 18) //删除Avenir和Next之间的空间

  1. 问题:2和3

将此扩展名添加到您的项目中

extension UIImageView {

    func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float)
    {
        layer.masksToBounds = false
        layer.shadowOffset = offset
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
    }
}

并且这样称呼

let wishlistImage: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.image = UIImage(named: "logoGroß")
        v.layer.cornerRadius = 3.0
        v.addShadow(offset: CGSize(width: 3, height: 3), color: UIColor.black, radius: 2.0, opacity: 1.0)
        return v
    }()

0
投票

关于您的第一个问题,如@ pigeon_39所述,您应该写上字体名称而不能使用空格。

对于第二和第三个问题,您可以使用此解决方案:

let wishlistImage: UIImageView = {
    let v = UIImageView()
    let cornerRadius: CGFloat = 5
    v.layer.shadowPath = UIBezierPath(roundedRect: v.bounds, cornerRadius: cornerRadius).cgPath
    v.layer.shadowRadius = cornerRadius
    v.layer.shadowOffset = .zero
    v.layer.shadowOpacity = 0.3
    v.layer.shadowRadius = 10
    v.layer.cornerRadius = cornerRadius
    v.layer.shadowColor = UIColor.black.cgColor
    return v
}
© www.soinside.com 2019 - 2024. All rights reserved.