gpt4 book ai didi

ios - UILabel 上的 instrictContentSize 在不同的实例中返回不同的值

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

我正在使用 swift 3.0 开发一个项目,我在 UIStoryBoard 中有三个 numberOFLines 设置为 6 的 UILabel。在标签下方,我放置了三个 UIButton,它们用作“查看更多”选项。有时我的代码工作正常,如果我截断的标签显示“查看更多”按钮,一旦单击它会显示全部内容,而有时即使 UILabel 内容被截断它仍然不显示“查看更多”按钮所以我'我无法看到全部内容。我在代码中缺少什么,帮助将不胜感激。

   import UIKit

class MP3ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet var descriptionSeeMoreBtn: UIButton!

@IBOutlet var howToUseSeeMoreBtn: UIButton!

@IBOutlet var cautionsSeeMreBtn: UIButton!
var seeMoreIsShowing = false
@IBOutlet var cautionsContentLbl: UILabel!
@IBOutlet var howToUseContentLbl: UILabel!
@IBOutlet var descriptionContentLbl: UILabel!

override func viewDidLoad() {
self.descriptionContentLbl.sizeToFit()
self.howToUseContentLbl.sizeToFit()
self.cautionsContentLbl.sizeToFit()
getItButton.layer.cornerRadius = 5
activityIndicator.startAnimating()
let mediaID = mediaDetails?["entity_key"] as! String
let url = URL(string: Config.MP3_LIST + "?mediaId=\(mediaID)")
let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

if(error != nil){
print(error!);
DispatchQueue.main.sync(execute: {
self.activityIndicator.stopAnimating()
})
}
else{
do{
if let urlContent = data {
let serverResponseData = try (JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary
if(serverResponseData["error"] == nil){
self.mediaDetails = serverResponseData
print("Media :",self.mediaDetails!)
self.mediaList = (self.mediaDetails?["trackList"]as? NSArray)!

DispatchQueue.main.sync(execute: {

self.descriptionContentLbl.text = self.mediaDetails?["description"] as? String ?? "description...."
self.howToUseContentLbl.text = self.mediaDetails?["howToUse"] as? String ?? "How to use......."
self.cautionsContentLbl.text = self.mediaDetails?["cautions"] as? String ?? "cautions...."
let track = ((self.mediaDetails?["trackList"] as! NSArray)[0]) as! NSDictionary
self.activityIndicator.stopAnimating()
})
}
}
}
catch {
print("Error In Json De-serialization")
}
DispatchQueue.main.sync(execute: {
self.activityIndicator.stopAnimating()
})
}
})
task.resume();

}

override func viewDidAppear(_ animated: Bool) {
loadTheInitialLabelText()
self.tableView.tableFooterView = UIView(frame: .zero)
}

@IBAction func descriptionSeeMoreButtonPressed(_ sender: Any) {
if (seeMoreIsShowing) {
self.descriptionContentLbl.numberOfLines = 6
self.descriptionSeeMoreBtn.setTitle("see more", for: .normal)
}else {
self.descriptionContentLbl.numberOfLines = 0
self.descriptionSeeMoreBtn.setTitle("show less", for: .normal)
}
seeMoreIsShowing = !seeMoreIsShowing
}
@IBAction func howToUSeSeeMoreButtonPressed(_ sender: Any) {
if (seeMoreIsShowing) {
self.howToUseContentLbl.numberOfLines = 6
self.howToUseSeeMoreBtn.setTitle("see more", for: .normal)
}else {
self.howToUseContentLbl.numberOfLines = 0
self.howToUseSeeMoreBtn.setTitle("show less", for: .normal)
}
seeMoreIsShowing = !seeMoreIsShowing
}


@IBAction func cautionsSeeMoreButtonPressed(_ sender: Any) {
if (seeMoreIsShowing) {
self.cautionsContentLbl.numberOfLines = 6
self.cautionsSeeMreBtn.setTitle("see more", for: .normal)
}else {
self.cautionsContentLbl.numberOfLines = 0
self.cautionsSeeMreBtn.setTitle("show less", for: .normal)
}
seeMoreIsShowing = !seeMoreIsShowing
}

func loadTheInitialLabelText() {
let DescriptionTextheight = self.descriptionContentLbl.text?.height(withConstrainedWidth: self.descriptionContentLbl.frame.width, font: self.descriptionContentLbl.font)
if self.descriptionContentLbl.intrinsicContentSize.height < DescriptionTextheight! {
self.descriptionSeeMoreBtn.isHidden = false
}else{
self.descriptionSeeMoreBtn.isHidden = true
}

let howToUseTextheight = self.howToUseContentLbl.text?.height(withConstrainedWidth: self.howToUseContentLbl.frame.width, font: self.howToUseContentLbl.font)
if self.howToUseContentLbl.intrinsicContentSize.height < howToUseTextheight! {
self.howToUseSeeMoreBtn.isHidden = false
}else{
self.howToUseSeeMoreBtn.isHidden = true
}

let cautionsTextheight = self.cautionsContentLbl.text?.height(withConstrainedWidth: self.cautionsContentLbl.frame.width, font: self.cautionsContentLbl.font)
if self.cautionsContentLbl.intrinsicContentSize.height < cautionsTextheight! {
self.cautionsSeeMreBtn.isHidden = false
}else{
self.cautionsSeeMreBtn.isHidden = true
}

}

}
extension String {

func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)

return boundingBox.height
}
}

最佳答案

你在设置它们的文本值之前调整你的标签大小,在设置它们的值之后调整大小。但这并不能解决问题。

我相信可能发生的情况是您的网络服务返回时间太长。因此,当您的网络服务正在等待时,您的程序的其余部分将继续执行。

因此,您最终会比较空标签并获取它们的高度,然后获取这些标签的固有内容大小。固有大小是非文本填充标签的大小,它小于您的函数为该标签计算的任何高度。这最终会隐藏您的按钮。

之后,URLSession 会为您提供数据并设置标签的文本值,但高度计算已经发生。

您可以尝试的快速解决方法是在设置标签文本后,在 ViewDidLoad URLSession 处理程序中调用 LoadInitialText()。

关于ios - UILabel 上的 instrictContentSize 在不同的实例中返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752397/

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