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

标签 ios swift uiimage uilabel uicollectionviewcell

我想自定义我的 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。问题 - 为什么标签不是粗体

<强>2。问题 - 为什么没有cornerRadius

<强>3。问题 - 为什么 shadow 没有出现(底部 + 右侧)?

最佳答案

对于你的第一个问题,@pigeon_39 说你应该在没有空格的情况下写下你的字体名称。

对于第二个和第三个问题,可以使用这个解决方法:

func addShadow() {
    let cornerRadius: CGFloat = 5
    self.wishlistImage.layer.shadowPath = UIBezierPath(roundedRect: self.wishlistImage.bounds, cornerRadius: cornerRadius).cgPath
    self.wishlistImage.layer.shadowRadius = cornerRadius
    self.wishlistImage.layer.shadowOffset = .zero
    self.wishlistImage.layer.shadowOpacity = 0.3
    self.wishlistImage.layer.shadowRadius = 10
    self.wishlistImage.layer.cornerRadius = cornerRadius
    self.wishlistImage.layer.shadowColor = UIColor.black.cgColor
}

并在设置约束后调用。

关于ios - Swift - 自定义单元格内容(UILabel、UIImageView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679999/

相关文章:

objective-c - CGLayer绘图随着时间的过去而变慢

ios - paymentRequestWithMerchantIdentifier NSInvalidArgumentException 错误

ios - UIImage initWithData : blocking UI thread from async dispatch?

ios - 在 Button Click Swift 上重复附加到数组

objective-c - MKAnnotation 未在 MKMapView 上显示标注

swift - 设置 UIImageView 宽度、高度、长宽比和约束 X、Y

ios - 作为构造函数关闭?

ios - 样本: Tableview of custom cells to another tableview of custom cells

objective-c - ios-在 uiimage 元数据中添加地理定位

ios - 为什么屏幕截图图像会拉伸(stretch)?