页面控件中的多个 UILabel
Multiple UILabel in Page Control
我正在尝试为多个 UILabel 做一个 scrollView + pageControl,即每个视图应该有 2 个 UILabel - 一个用于标题,一个用于描述。
我熟悉将单个 UILabel 添加到 scrollView + pageControl 中。问题是如何将多个 UILabel 添加到 scrollView 的子视图中?我有以下代码,适用于单个 UILabel:
@IBOutlet weak var bloggerReviewScrollView: UIScrollView!
@IBOutlet weak var bloggerReviewPageControl: UIPageControl!
var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
var reviews: [Reviews] = [Reviews(name: "NameA", description: "DescriptionA", date: Date.init(), url: nil), Reviews(name: "NameB", description: "DescriptionB", date: Date.init(), url: nil)]
func updateReview() {
reviewPageControl.numberOfPages = reviews.count
for index in 0..<reviews.count {
frame.origin.x = reviewScrollView.frame.size.width * CGFloat(index)
frame.size = reviewScrollView.frame.size
let reviewContent = UILabel(frame: frame)
reviewContent.text = reviews[index].description
reviewContent.numberOfLines = 0
reviewScrollView.addSubview(reviewContent)
}
reviewScrollView.contentSize = CGSize(width: reviewScrollView.frame.size.width * CGFloat(reviews.count), height: reviewScrollView.frame.size.height)
reviewScrollView.delegate = self
}
但是,除了 reviews[index].description
,我还想在不同的标签中显示 reviews[index].name
,以便该标签的样式独立于视图中描述标签的样式。我有什么办法可以这样做吗?
非常感谢!
您需要决定在何处以及如何显示两个标签,即标签框架,我将其分为两部分,您可以根据需要更改框架并创建新的 Label 实例并将其添加到您的评论ScrollView
func updateReview() {
reviewPageControl.numberOfPages = reviews.count
for index in 0..<reviews.count {
frame.origin.x = reviewScrollView.frame.size.width * CGFloat(index)
frame.size = reviewScrollView.frame.size
//title lable
////set title frame based on your requirment
let reviewLabelFrame = CGRect(x: frame.origin.x, y: 0, width: frame.width, height: frame.height/2)
let titleLabel = UILabel(frame: reviewLabelFrame)
titleLabel.text = reviews[index]. name // get name and set the title
titleLabel.numberOfLines = 0
reviewScrollView.addSubview(titleLabel)
//descriptions Label
//set descriptions frame based on your requirment
let desLableFram = CGRect(x: frame.origin.x, y: frame.height/2, width: frame.width, height: frame.height/2)
let desLable = UILabel(frame: desLableFram)
desLable.text = reviews[index].description //get description and set
desLable.numberOfLines = 0
reviewScrollView.addSubview(desLable) //add your descriptions
}
您需要根据您的要求计算两个标签的帧
我正在尝试为多个 UILabel 做一个 scrollView + pageControl,即每个视图应该有 2 个 UILabel - 一个用于标题,一个用于描述。
我熟悉将单个 UILabel 添加到 scrollView + pageControl 中。问题是如何将多个 UILabel 添加到 scrollView 的子视图中?我有以下代码,适用于单个 UILabel:
@IBOutlet weak var bloggerReviewScrollView: UIScrollView!
@IBOutlet weak var bloggerReviewPageControl: UIPageControl!
var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
var reviews: [Reviews] = [Reviews(name: "NameA", description: "DescriptionA", date: Date.init(), url: nil), Reviews(name: "NameB", description: "DescriptionB", date: Date.init(), url: nil)]
func updateReview() {
reviewPageControl.numberOfPages = reviews.count
for index in 0..<reviews.count {
frame.origin.x = reviewScrollView.frame.size.width * CGFloat(index)
frame.size = reviewScrollView.frame.size
let reviewContent = UILabel(frame: frame)
reviewContent.text = reviews[index].description
reviewContent.numberOfLines = 0
reviewScrollView.addSubview(reviewContent)
}
reviewScrollView.contentSize = CGSize(width: reviewScrollView.frame.size.width * CGFloat(reviews.count), height: reviewScrollView.frame.size.height)
reviewScrollView.delegate = self
}
但是,除了 reviews[index].description
,我还想在不同的标签中显示 reviews[index].name
,以便该标签的样式独立于视图中描述标签的样式。我有什么办法可以这样做吗?
非常感谢!
您需要决定在何处以及如何显示两个标签,即标签框架,我将其分为两部分,您可以根据需要更改框架并创建新的 Label 实例并将其添加到您的评论ScrollView
func updateReview() {
reviewPageControl.numberOfPages = reviews.count
for index in 0..<reviews.count {
frame.origin.x = reviewScrollView.frame.size.width * CGFloat(index)
frame.size = reviewScrollView.frame.size
//title lable
////set title frame based on your requirment
let reviewLabelFrame = CGRect(x: frame.origin.x, y: 0, width: frame.width, height: frame.height/2)
let titleLabel = UILabel(frame: reviewLabelFrame)
titleLabel.text = reviews[index]. name // get name and set the title
titleLabel.numberOfLines = 0
reviewScrollView.addSubview(titleLabel)
//descriptions Label
//set descriptions frame based on your requirment
let desLableFram = CGRect(x: frame.origin.x, y: frame.height/2, width: frame.width, height: frame.height/2)
let desLable = UILabel(frame: desLableFram)
desLable.text = reviews[index].description //get description and set
desLable.numberOfLines = 0
reviewScrollView.addSubview(desLable) //add your descriptions
}
您需要根据您的要求计算两个标签的帧