Ios Swift-自定义单元格内容(UILabel、UIImageView)
我想自定义我的Ios Swift-自定义单元格内容(UILabel、UIImageView),ios,swift,uiimage,uilabel,uicollectionviewcell,Ios,Swift,Uiimage,Uilabel,Uicollectionviewcell,我想自定义我的UICollectionViewCell,但它无法按我希望的方式工作 这是我的第一个单元格代码: class MainWishlistCell: UICollectionViewCell { let wishlistImage: UIImageView = { let v = UIImageView() v.translatesAutoresizingMaskIntoConstraints = false v.image =
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),
])
}
}
看起来是这样的:
1。问题-为什么标签不加粗
2。问题-为什么没有转弯半径
3。问题-为什么阴影不出现(底部+右侧)?试试这个
v.font=UIFont(名称:“AvenirNext粗体”,大小:18)
//删除Avenir和Next之间的空格
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
}()
试试这个
v.font=UIFont(名称:“AvenirNext粗体”,大小:18)
//删除Avenir和Next之间的空格
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
}()
对于你的第一个问题,@bige_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
}
设置约束后调用它。对于您的第一个问题,如@鸽子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
}
设置约束后调用它。除了拐角半径之外,所有操作都非常有效。你知道我该如何解决这个问题吗?对于圆角,你应该设置
layer.masksToBound=true
,对于阴影,你应该设置false
!这不是为圆形图像添加阴影的解决方案。你知道解决方案吗?还有一件事,我尝试将字体改为medium
,而不是bold
,但不知怎的,AvenirNext medium
不起作用。除了圆角半径之外,其他一切都很好。你知道我该如何解决这个问题吗?对于圆角,你应该设置layer.masksToBound=true
,对于阴影,你应该设置false
!这不是为圆形图像添加阴影的解决方案。你知道解决方案吗?还有一件事,我尝试将字体改为medium
,而不是bold
,但不知何故AvenirNext medium
不起作用。设置约束后,你的意思是什么?在func commonInit()
之后调用addShadow()
对我不起作用。我的意思是在激活constraint之后调用commonInit()
,这对你有效??标签周围有一个阴影,但图像没有任何变化:你敢肯定你对wishListImage
vswishListLabel
的看法是正确的吗?正如你所看到的,我是为wishListImage
写的!对我将roundRect:
设置为self.wishlistImage.bounds
。我以前就有过自我。。现在,它不再出现了。设置约束后,您的意思是什么?在func commonInit()
之后调用addShadow()
对我不起作用。我的意思是在激活constraint之后调用commonInit()
,这对你有效??标签周围有一个阴影,但图像没有任何变化:你敢肯定你对wishListImage
vswishListLabel
的看法是正确的吗?正如你所看到的,我是为wishListImage
写的!对我将roundRect:
设置为self.wishlistImage.bounds
。我以前就有过自我。。现在它不再出现了