Skip to content

Instantly share code, notes, and snippets.

View khanhbui's full-sized avatar

Khanh Bui khanhbui

  • Hanoi - Vietnam
View GitHub Profile
@khanhbui
khanhbui / FakeNavigationBar.swift
Created March 29, 2019 04:54 — forked from fuxingloh/FakeNavigationBar.swift
Add fake navigation bar, remove, revert, load, unload and change status tint color, fake navigation bar can be used to assist transition from a controller with navigation background to one without.
extension UIViewController {
// Nav tag id
private var fakeNavTag: Int{get{return 80327}}
// Remove bar bar color and add fake nav bar
public func loadFakeNavigationBar(){
removeNavigationBarBackground()
addFakeNavigationBar()
}
@khanhbui
khanhbui / PushDissolveAnimationSegue.swift
Created March 29, 2019 04:51 — forked from fuxingloh/PushDissolveAnimationSegue.swift
iOS Swift: A PushDissolveAnimationSegue with a dissolve animation,
class PushDissolveAnimationSegue: UIStoryboardSegue {
override func perform() {
let transition = CATransition()
transition.duration = 0.2
transition.type = kCATransitionFade
self.sourceViewController.navigationController!.view.layer.addAnimation(transition, forKey: kCATransition)
self.sourceViewController.navigationController!.pushViewController(self.destinationViewController, animated: false)
@khanhbui
khanhbui / UICollectionView.swift
Created March 29, 2019 04:49 — forked from fuxingloh/UICollectionView.swift
iOS Swift: How to count the width of the UILabel. And how to size UICollectionViewCell dynamically with label.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = collections[indexPath.row].name
let width = UILabel.textWidth(font: titleFont, text: text)
return CGSize(width: width + left + right, height: height)
}
@khanhbui
khanhbui / Gradient.swift
Created March 29, 2019 04:47 — forked from fuxingloh/Gradient.swift
How to add gradient on top and bottom of image view.
// Call this in layoutSubView or viewDidLoad
self.imageView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
let width = self.bounds.width
let height = self.bounds.height
let sHeight:CGFloat = 60.0
let shadow = UIColor.black.withAlphaComponent(0.6).cgColor
// Add gradient bar for image on top
let topImageGradient = CAGradientLayer()
@khanhbui
khanhbui / UILabelSize.swift
Created March 29, 2019 04:41 — forked from fuxingloh/UILabelSize.swift
iOS Swift: How to find text width, text height or size of UILabel.
extension UILabel {
func textWidth() -> CGFloat {
return UILabel.textWidth(label: self)
}
class func textWidth(label: UILabel) -> CGFloat {
return textWidth(label: label, text: label.text!)
}
class func textWidth(label: UILabel, text: String) -> CGFloat {
@khanhbui
khanhbui / UILabelCountLines.swift
Created March 29, 2019 04:40 — forked from fuxingloh/UILabelCountLines.swift
iOS Swift: How to check if UILabel is truncated? Calculate number of lines for UILabel
func countLabelLines(label: UILabel) -> Int {
// Call self.layoutIfNeeded() if your view uses auto layout
let myText = label.text! as NSString
let rect = CGSize(width: label.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil)
return Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight))
}
@khanhbui
khanhbui / node-modules-in-react-native.md
Created April 23, 2018 03:20 — forked from parshap/node-modules-in-react-native.md
Running Node Modules in React Native

Running Node Modules in React Native

How to use packages that depend on Node.js core modules in React Native.

Node.js Core Modules

Node has several modules that are automatically available via require(): http, crypto, etc. While these modules are available in Node, they are not automatically available in other environments

@khanhbui
khanhbui / gist:001d372c351e4e9aa4aa8b36ff92bf05
Created May 19, 2017 06:37 — forked from igaiga/gist:1308730
Set UIWebView's user agent into NSMutableURLRequest
NSString *urlString = [NSString stringWithFormat:@"http://example.com/foo"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// get User Agent in UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"UserAgent: %@", userAgent);
[webView release];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response;
@khanhbui
khanhbui / KDJConfig.h
Created August 24, 2016 03:54 — forked from kristopherjohnson/KDJConfig.h
Objective-C class which retrieves values from a resource file Config.plist. Similar to NSUserDefaults, but read-only.
// Interface to the values stored in the Config.plist resource file
@interface KDJConfig : NSObject
// Return singleton instance
+ (KDJConfig *)sharedConfig;
// Return string value with specified key from Config.plist, or default value if not specified in the file
- (NSString *)stringForKey:(NSString *)key defaultValue:(NSString *)defaultValue;
@khanhbui
khanhbui / GLTextureView.java
Created August 22, 2016 08:20 — forked from mkazuma/GLTextureView.java
Test implementation of GLTextureView offering completely same APIs as GLSurfaceView. Based on the article below. http://stackoverflow.com/questions/12061419/converting-from-glsurfaceview-to-textureview-via-gltextureview
package org.cocos2dx.lib;
import java.io.Writer;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;