gpt4 book ai didi

ios - 如何在 Swift 的分段控件中调整 textLabel 的大小?

转载 作者:行者123 更新时间:2023-11-28 11:57:56 25 4
gpt4 key购买 nike

UIViewController 中,有一个位于 View 顶部的分段 Controller 。约束:左:0,右:0,上:0,高:90。

每次发布​​ UIApplicationDidBecomeActive 通知时,分段控件应通过调用 func updateUI() 以最新日期更新它的 View

在第一次加载时,当 viewDidLoad 被调用时,每个段中的文本都按预期显示。
但是,如果应用程序进入后台,当它返回前台时,分段控件的标签不容纳文本并显示 3 个点...之后。请参阅下面的附件和 gif。

范围:每次发布 .UIApplicationDidBecomeActive 通知时更新每个段的分段控件标题。 公共(public) 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()
}

enter image description here enter image description here

最佳答案

该问题仅在 10.3 iOS 版本中重现。解决方案:

存储分段标签的初始属性:

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

getNextDays 之前添加到 ViewDidLoad:

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)
}

Sample code with some crash protection

关于ios - 如何在 Swift 的分段控件中调整 textLabel 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50579137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com