簡體   English   中英

如何在Swift的分段控件中調整textLabel的大小?

[英]How to resize textLabel in segmented control in Swift?

UIViewController ,在視圖頂部放置了一個分段控制器。 限制條件:左:0,右:0,上:0,高:90。

每次發布UIApplicationDidBecomeActive通知時,分段控件都應通過調用func updateUI()將其視圖更新為最新日期。

首次加載時,調用viewDidLoad時,將按預期顯示每個段中的文本。
但是,如果應用程序在后台運行,則在回到前台時,分段控件的標簽將不容納文本,並在后面顯示3個點。 請參閱下面的附件和gif。

范圍:每次發布.UIApplicationDidBecomeActive通知時,更新每個段的分段控件標題。 公共Github項目https://github.com/bibscy/testProject

 class ViewController: UIViewController {

   var stringDates = [String]()

   var dateNow: Date {
     return Date()
   }
      @IBOutlet weak var segmentedControl: UISegmentedControl!

       func updateUI() {
    //create string dates and set the title of for each segment in segmented control
          self.getNextDays()  
     }

   override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: .UIApplicationDidBecomeActive, object: nil)

     UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0

      self.getNextDays()
   }
}


  extension ViewController {

//for each segment, construct a string date with the currentDate() being first, followed by 5 consecutive days
func getNextDays()  {

    stringDates.removeAll() //clear any previous data

    for i in 0...5 {
        let dateFormatter = DateFormatter()
        let today = dateNow
        let calendar = Calendar.current

        if i == 0 {
            let dayComponent = Calendar.current.component(.weekday,from: today)
            let day = Calendar.current.component(.day, from: today) //Int
            let dayString = dateFormatter.shortWeekdaySymbols[dayComponent-1] //Symbol


            let month = Calendar.current.component(.month, from: today)
            let monthSymbol = dateFormatter.shortMonthSymbols[month-1]
            let dayMonthString =  dayString + " " + String(day) + " " + monthSymbol
            stringDates.append(dayMonthString)


        } else {
            var components = DateComponents()
            components.weekday = i

            let nextDay = calendar.date(byAdding: components, to: today)
            let nextDayComponent = Calendar.current.component(.weekday,from: nextDay!)
            let day = Calendar.current.component(.day, from: nextDay!)
            let dayString = dateFormatter.shortWeekdaySymbols[nextDayComponent-1] // Symbol


            let month = Calendar.current.component(.month, from: nextDay!)
            let monthSymbol = dateFormatter.shortMonthSymbols[month-1]
            let dayMonthString = dayString + " " + String(day) + " " + monthSymbol
            stringDates.append(dayMonthString)
        }
    }


      //set the title for each segment from stringDates array
       for value in 0...5 {
         self.segmentedControl.setTitle(self.stringDates[value], forSegmentAt: value)
      }
   } //end of getNextDays()  
}

在此處輸入圖片說明 在此處輸入圖片說明

該問題僅在10.3 iOS版本中再現。 解:

存儲分段標簽的初始屬性:

fileprivate var height: CGFloat! 
fileprivate var width: CGFloat!

在getNextDays之前添加到getNextDays

let label = segmentedControl.subviews[0].subviews[0] as! UILabel 
height = label.frame.height 
width = label.frame.width

設定文字:

for value in 0...5 {
   segmentedControl.setTitle(stringDates[value], forSegmentAt: value)
}

然后修復框架:

let labels = segmentedControl.subviews.map{ $0.subviews[1] } as! [UILabel]
    labels.forEach { (label) in
        label.numberOfLines = 0
        let frame = label.frame
        label.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: width, height: height)
    }

帶有崩潰保護的示例代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM