我正在使用swift 3.0中的一个项目,我在UIStoryBoard中有三个UILabel,numberOFLines设置为6。在标签下方,我放置了三个UIButton,其功能为"请参阅更多"选项。有时我的代码工作得很好,如果标签我截断它显示看到更多按钮,一旦点击它显示整个内容,而有时即使UILabel内容是截断它仍然没有显示"看到更多&#34 ;按钮,所以我无法看到整个内容。我在代码中缺少什么,帮助会非常感激。
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
}
}
答案 0 :(得分:1)
你在设置文本值之前设置尺寸,以便在设置值后设置sizeToFit。仅此一点不会解决问题。
我相信可能发生的事情是您的网络服务需要很长时间才能返回。因此,当您的Web服务正在等待时,程序的其余部分将继续执行。
因此,您最终会比较空标签并获取其高度,然后获取这些标签的内在内容大小。内在大小是非文本填充标签的大小,并且小于您的函数为该标签计算的任何高度。这最终隐藏了你的按钮。
然后,URLSession会为您提供数据并设置标签的文本值,但高度计算已经发生。
您可以尝试的快速修复是在设置标签文本后调用ViewDidLoad URLSession处理程序中的LoadInitialText()。