Ios UILabel上的instrictContentSize在不同的实例中返回不同的值
我正在swift 3.0中的一个项目中工作,在UIStoryBoard中有三个UILabel,numberOFLines设置为6。在标签下面,我放置了三个UIButton,作为“查看更多”选项。有时我的代码工作正常,如果我截断标签,它会显示“查看更多”按钮,单击后会显示整个内容,而有时即使UILabel内容被截断,它仍然不会显示“查看更多”按钮,因此我无法看到整个内容。我在代码中遗漏了什么,请帮忙Ios UILabel上的instrictContentSize在不同的实例中返回不同的值,ios,swift,uilabel,Ios,Swift,Uilabel,我正在swift 3.0中的一个项目中工作,在UIStoryBoard中有三个UILabel,numberOFLines设置为6。在标签下面,我放置了三个UIButton,作为“查看更多”选项。有时我的代码工作正常,如果我截断标签,它会显示“查看更多”按钮,单击后会显示整个内容,而有时即使UILabel内容被截断,它仍然不会显示“查看更多”按钮,因此我无法看到整个内容。我在代码中遗漏了什么,请帮忙 import UIKit class MP3ViewController: UIViewC
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
}
}
导入UIKit
类MP3ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
@IBOutlet var描述查看更多BTN:UIButton!
@IBOUTLE var HOWTUSESEEMOREBTN:UIButton!
@IBOUTLE var警告重新发布:UIButton!
var seeMoreIsShowing=false
@IBOUTLE var警戒内容BL:UILabel!
@IBOUTLE var HOWTUSECONTENTLBL:UILabel!
@IBOutlet var descriptionContentBL:UILabel!
重写func viewDidLoad(){
self.descriptionContentBl.sizeToFit()的名称
self.howtuseContentlbl.sizeToFit()文件
self.cautionsContentBl.sizeToFit()的
getItButton.layer.cornerRadius=5
activityIndicator.startAnimating()
让mediaID=mediaDetails?[“实体_键”]作为!字符串
让url=url(字符串:Config.MP3\u LIST+“?mediaId=\(mediaId)”)
让task=URLSession.shared.dataTask(带:url!,completionHandler:{(数据,响应,错误)在
如果(错误!=nil){
打印(错误!);
DispatchQueue.main.sync(执行:{
self.activityIndicator.stopAnimating()
})
}
否则{
做{
如果让urlContent=data{
让serverResponseData=try(JSONSerialization.jsonObject(with:urlContent,options:JSONSerialization.ReadingOptions.mutableContainers))作为!NSDictionary
if(serverResponseData[“error”]==nil){
self.mediaDetails=serverResponseData
打印(“媒体:,self.mediaDetails!”)
self.mediaList=(self.mediaDetails?[“trackList”]as?NSArray)!
DispatchQueue.main.sync(执行:{
self.descriptionContentBL.text=self.mediaDetails?[“描述”]作为?字符串???“描述…”
self.howToUseContentLbl.text=self.mediaDetails?[“howToUse”]作为?字符串??“如何使用……”
self.cautionscontetlbl.text=self.mediaDetails?[“注意事项”]作为?字符串??“注意事项…”
让track=((self.mediaDetails?[“trackList”]as!NSArray)[0])as!NSDictionary
self.activityIndicator.stopAnimating()
})
}
}
}
抓住{
打印(“Json反序列化错误”)
}
DispatchQueue.main.sync(执行:{
self.activityIndicator.stopAnimating()
})
}
})
task.resume();
}
覆盖函数视图显示(u动画:Bool){
加载初始标签文本()
self.tableView.tableFooterView=UIView(帧:.0)
}
@iAction func description查看更多按钮已按下(\发送方:任何){
如果(请参见更多信息显示){
self.descriptionContentLbl.numberOfLines=6
self.descriptionSeeMoreBtn.setTitle(“查看更多”,用于:。正常)
}否则{
self.descriptionContentBL.numberOfLines=0
self.descriptionSeeMoreBtn.setTitle(“显示较少”,表示:。正常)
}
seeMoreIsShowing=!seeMoreIsShowing
}
@iAction func HowtouseSemoRebuttonPressed(\发送方:任何){
如果(请参见更多信息显示){
self.howtusecontentlbl.numberOfLines=6
self.howtuseseemorebtn.setTitle(“查看更多”,用于:。正常)
}否则{
self.howtusecontentlbl.numberOfLines=0
self.howtuseseemorebtn.setTitle(“显示较少”,表示:。正常)
}
seeMoreIsShowing=!seeMoreIsShowing
}
@iAction func告诫删除拒绝已发布(uu发件人:任何){
如果(请参见更多信息显示){
self.cautionsContentBL.numberOfLines=6
self.cautionsSeeMreBtn.setTitle(“请参阅更多”,用于:。正常)
}否则{
self.cautionsContentBL.numberOfLines=0
self.cautionsSeeMreBtn.setTitle(“显示较少”,用于:。正常)
}
seeMoreIsShowing=!seeMoreIsShowing
}
func加载初始标签文本(){
让DescriptionTextheight=self.DescriptionContentBl.text?.height(带约束宽度:self.DescriptionContentBl.frame.width,字体:self.DescriptionContentBl.font)
如果self.descriptionContentBl.intrinsicContentSize.height