asked    Lyndon     2018-10-22       ios       81 view        1 Answer

[SOLVED] Swift 4, iOS how to detect label height after added string value

in UICollectionView my cell's size must be 150+label.height. Now I'm using custom layout to calculate height = 150 if image exists inImageView if not height = 100. It's easy. But how to calculate label.height when i don't know what text i'll fetch from Api? Is there any function to get that?

customLayout.swift

let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath) > 0 ? 150 : 100
let height = cellPadding * 2 + CGFloat(photoHeight) + (labelHeight??????)

  1 Answer  

        answered    Elma     2018-10-22      

You can calculate label height using below function

extension UILabel {

    func heightToFitContent() -> CGFloat {

        self.numberOfLines = 0
        self.lineBreakMode = NSLineBreakMode.byWordWrapping
        let maximumLabelSize : CGSize = CGSize(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude)
        let expectedLabelSize : CGSize = self.sizeThatFits(maximumLabelSize);


        self.sizeToFit()

        return expectedLabelSize.height
    }
}

call this function in UI thread. Hope this helps.





Your Answer





 2018-10-22         Mignon

Angular: Is there a way to mock the value of PLATFORM_ID in a unit test?

I am using Angular Universal. I have a guard for a route that behaves differently depending on if I am running on the server or the browser platform. Here is the guard:export class UniversalShellGuard implements CanActivate { private isBrowser: boolean; constructor(@Inject(PLATFORM_ID) private platformId: Object) { console.log('PLATFORM_ID = ' + platformId); this.isBrowser = isPlatformBrowser(this.platformId); } canActivate(): Observable<boolean> | Promise<boolean> | boolean { return !this.isBrowser; }}As you can see, the guard is injecting PLATFOR...
 angular                     1 answers                     57 view
 2018-10-22         Wilbur

send token to server in angular universal

I have an Angular 6+ app which is configured to utilize server side rendering using Angular Universal.I also used TransferState to avoid duplicate API calls on the server and client app.Authentication in my angular app is based on token.The problem is in the first time the user opens my web app, which results to rendering index.html for a user which is not authenticated whereas the user actually is logged in but there is no opportunity to transfer the token to the server. So, when the client app swapped with server app, it is required to call APIs again because of the exist...
 node.js                     1 answers                     60 view
 2018-10-22         Geoffrey

Kubernetes TLS issue

Im having some issues when building my Kubernetes Cluster.Whenever I try to run a kubectl command, or my ansible scrips does, it gets the following error: Unable to connect to the server: tls: either ServerName or InsecureSkipVerify must be specified in the tls.ConfigNot from a script or by hand can anything execute the kubectl command, yet the apiserver pod is running.Any help will be greatly appreciated. You probably either haven't specified the server name or the CA certificate, or specified an incorrect one. Make sure you have specified the correct CA and server ...
 kubernetes                     1 answers                     62 view