页面控件中的多个UILabel

时间:2018-06-29 14:06:10

标签: swift uiscrollview uilabel uipagecontrol

我正在尝试为多个UILabel做一个scrollView + pageControl,即每个视图应具有2个UILabel-一个用于标题,一个用于描述。

我熟悉将单个UILabel添加到scrollView + pageControl中。问题是如何在滚动视图的子视图中添加多个UILabel?我有以下代码对单个UILabel可以正常工作:

with t (str) as (
            select 'PREPROCESSINGLIST_AOD' from dual
  union all select 'PREPROCESSINGLIST_BOD' from dual
  union all select 'PREPROCESSINGLIST_COD' from dual
  union all select 'PREPROCESSINGLIST_DOD' from dual
  union all select 'XYZ_PREPROCESSINGLIST_EOD' from dual
  union all select 'XYZ_FOD' from dual
  union all select 'ABC_XYZ_GOD' from dual
  union all select 'HOD' from dual
)
select str,
  regexp_replace(str, '^PREPROCESSINGLIST_', null) as anchor_regex,
  regexp_replace(str, 'PREPROCESSINGLIST_', null) as free_regex,
  replace(str, 'PREPROCESSINGLIST_', null) as free_replace,
  case when instr(str, '_') > 0 then substr(str, instr(str, '_') + 1) else str end
    as first_underscore,
  case when instr(str, '_') > 0 then substr(str, instr(str, '_', -1) + 1) else str end
    as last_underscore
from t;

STR                       ANCHOR_REGEX              FREE_REGEX  FREE_REPLAC FIRST_UNDERSCORE      LAST_UNDERS
------------------------- ------------------------- ----------- ----------- --------------------- -----------
PREPROCESSINGLIST_AOD     AOD                       AOD         AOD         AOD                   AOD        
PREPROCESSINGLIST_BOD     BOD                       BOD         BOD         BOD                   BOD        
PREPROCESSINGLIST_COD     COD                       COD         COD         COD                   COD        
PREPROCESSINGLIST_DOD     DOD                       DOD         DOD         DOD                   DOD        
XYZ_PREPROCESSINGLIST_EOD XYZ_PREPROCESSINGLIST_EOD XYZ_EOD     XYZ_EOD     PREPROCESSINGLIST_EOD EOD        
XYZ_FOD                   XYZ_FOD                   XYZ_FOD     XYZ_FOD     FOD                   FOD        
ABC_XYZ_GOD               ABC_XYZ_GOD               ABC_XYZ_GOD ABC_XYZ_GOD XYZ_GOD               GOD        
HOD                       HOD                       HOD         HOD         HOD                   HOD        

但是,除了@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,以便该标签的样式独立于视图中描述标签的样式。我有什么办法可以这样做吗?

谢谢你!

1 个答案:

答案 0 :(得分:1)

您需要确定两个标签的显示位置和方式,即标签框架,我分为两部分,您可以根据需要更改框架并创建新的Label实例,然后将其添加到您的reviewScrollView中。

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
}

您需要根据您的要求计算两个标签的框架