我是iOS开发的新手,正在构建自定义UIFlowLayout,目前正在构建由图像和标签组成的自定义单元对象。
我想在UIImage下绘制标签,但是我不知道如何直接在UIImage下锚定标签。
我要实现的布局是这样的:
===========================
[ Navigation Bar ]
===========================
[ Image ] [ Image ]
[ Label ] [ Label ]
[ Image ] [ Image ]
[ Label ] [ Label ]
[ Image ] [ Image ]
[ Label ] [ Label ]
===========================
[ Tab Bar ]
===========================
我当前用于
UICollectionViewCell
子类的代码如下:var label : UILabel!
var imageView : UIImageView!
override init(frame: CGRect)
{
// Initialize the frame object
super.init(frame: frame)
self.backgroundColor = UIColor.grayColor()
// Set up the image
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
contentView.addSubview(imageView)
// Make the rectangle a circle by adjusting the corner radius, relative to 50% of the bounds
self.layer.cornerRadius = (frame.size.width / 100) * 50
// Set up the label (how do I do the anchoring?)
label = UILabel(frame: frame)
label.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
label.textAlignment = .Center
label.text = "Alexander Sims"
contentView.addSubview(label)
}
最佳答案
设置标签的框架时,可以使用imageView.frame作为参考。
像这样:
let labelX = imageView.frame.origin.x
let labelY = imageView.frame.origin.y + imageView.frame.height
label = UILabel(frame: CGRectMake(labelX, labelY, imageView.frame.width, 21)) // The last variable, 21, is the label's height. Change as you see fit.
关于ios - SWIFT:在UIImage下绘制UILabel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27041050/