Skip to content

Instantly share code, notes, and snippets.

Avatar

WanFei WanFeiChen

View GitHub Profile
View UIRefreshControl
import UIKit
extension UIRefreshControl {
func beginRefreshingManually(_ tintColor:UIColor) {
self.tintColor = tintColor
if let scrollView = superview as? UIScrollView {
scrollView.setContentOffset(CGPoint(x: 0, y:scrollView.contentOffset.y - frame.height), animated: false)
}
beginRefreshing()
self.sendActions(for: .valueChanged)
View set tableHeaderView height dynamicly
let targetWidth = self.view.bounds.size.width;
let targetSize = CGSize(width: targetWidth, height: UIView.layoutFittingCompressedSize.height)
//如果headView中有通过aspect ratio决定高度的constraint,这将导致systemLayoutSizeFittingSize计算错误,所以在代码中把这种constraint转为固定高度的constraint
//see : https://stackoverflow.com/questions/44801492/systemlayoutsizefitting-ignoring-layout-constraints-and-using-image-view-intrins
let imageView = self.empyHeadView.subviews.last!
imageView.constraintWithIdentifier("heightConstraint")!.constant = (targetWidth - 40) * CGFloat(0.3);
//宽度固定,高度可变
let resultSize = self.empyHeadView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.defaultHigh, verticalFittingPriority:UILayoutPriority.fittingSizeLevel );
//set
View Custom UIAlertController's textColor and fonts
extension UIView {
func findDescendantViewsOfType<T: Any>(_ subViewType:T.Type) -> [T]{
var rs = [T]()
if self is T {
rs.append(self as! T)
}
for view in self.subviews {
rs.append(contentsOf: view.findDescendantViewsOfType(T.self))
}
@WanFeiChen
WanFeiChen / gist:c84a9cea868cfa774cddf4a3423cf64a
Last active Nov 1, 2019
Setting associated userObject to UIView
View gist:c84a9cea868cfa774cddf4a3423cf64a
//
// UIView+SF.swift
// WechatTail
//
// Created by CHENWANFEI on 2019/9/22.
// Copyright © 2019 SwordFish. All rights reserved.
//
import UIKit
@WanFeiChen
WanFeiChen / gist:2ec98991d16e7ce6a63b3207764e618b
Created Aug 17, 2019
How to use NSMetadataQuery in command line
View gist:2ec98991d16e7ce6a63b3207764e618b
let query = NSMetadataQuery();
query.searchScopes = ["/work/books"]
query.predicate = NSPredicate(format: "kMDItemFSName like %@", "NSHipster - Cocoa & Swift.pdf")
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: nil) { (_) in
print("count = \(query.resultCount)")
if let items = query.results as? [NSMetadataItem]{
for item in items{
if let path = item.value(forKey: NSMetadataItemPathKey) as? String{
print(path)
}
View UIViewController+SF.swift
//
// UIViewController+SF.swift
// BowenEdu
//
// Created by CHENWANFEI on 2018/8/21.
// Copyright © 2018 swordfish. All rights reserved.
//
import UIKit
extension UIViewController{
@WanFeiChen
WanFeiChen / Test.java
Last active Apr 7, 2020
Apache Tika + Tesseract-OCR to scan Chinese text in pdf
View Test.java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
@WanFeiChen
WanFeiChen / gist:f5cc44bf663f3722bd19097be47ccf9b
Last active Sep 11, 2020
let Android Activity's transition animation like iOS navigationController's push-pop one
View gist:f5cc44bf663f3722bd19097be47ccf9b
1.Override CommonActivity's startActivity and finish
@Override
public void startActivity(Intent intent) {
super.startActivity(intent);
overridePendingTransition(R.anim.from_right_in, R.anim.from_left_out);
}
@Override
public void finish() {
super.finish();
@WanFeiChen
WanFeiChen / gist:675de27e6ae59fd0bf20c49b87e72ed3
Last active Oct 13, 2017
Use AES/RSA encryption algorithm to make a KeyChain on Android
View gist:675de27e6ae59fd0bf20c49b87e72ed3
package com.swordfishsoft.security;
import android.content.Context;
import android.content.SharedPreferences;
import android.security.KeyPairGeneratorSpec;
import android.security.keystore.KeyProperties;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayInputStream;
You can’t perform that action at this time.