Skip to content

Instantly share code, notes, and snippets.

View yuhnag's full-sized avatar

yuhnag yuhnag

View GitHub Profile
@yuhnag
yuhnag / String+UTF16Pos.swift
Created April 27, 2020 13:30
UFT16の位置とStringの位置を変換する
import Foundation
extension String {
/// UTF16位置を文字位置に
func convertPosition(utf16pos: Int) -> Index? {
// UTF16のIndexを取得
let utf16view = self.utf16
let utf16index = utf16view.index(utf16view.startIndex, offsetBy: utf16pos)
return utf16index.samePosition(in: self)
@yuhnag
yuhnag / ViewController.swift
Last active January 22, 2019 12:44
AdMobのインターステンシャル広告の表示アニメーションを変更
class ViewController: UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
...
/// ビューの表示アニメーションを強制変更
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
viewControllerToPresent.modalTransitionStyle = .crossDissolve
@yuhnag
yuhnag / UIImage+GetAlphaImage.swift
Last active January 18, 2019 14:12
UIImageのAlphaを反転した画像を生成する
import UIKit
extension UIImage {
/// alphaチャネルだけ取得する
/// fillColor: 塗りつぶす色
/// invert: alphaを反転するか
func getAlphaImage(fillColor: UIColor = UIColor.white, invert: Bool = false) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
// 塗りつぶし
@yuhnag
yuhnag / WKWebView+ConsoleLog.swift
Last active January 22, 2019 15:42
WKWebViewでJavaScriptのconsole.logを使えるようにするextension
import WebKit
/// adding "console.log" support
extension WKWebView: WKScriptMessageHandler {
/// enabling console.log
public func enableConsoleLog() {
// set message handler
configuration.userContentController.add(self, name: "logging")
@yuhnag
yuhnag / ShareViewController.swift
Last active December 28, 2018 14:13
【自分用メモ】iOSのShare Extensionでファイルを渡す
override func didSelectPost() {
// PDFのファイルタイプ
let typeIdentifier = "com.adobe.pdf";
// データ取得
if let attachment = getFirstAttachement(extensionContext: self.extensionContext, typeIdentifier: typeIdentifier) {
attachment.loadFileRepresentation(forTypeIdentifier: typeIdentifier, completionHandler:
{(url, error) in // データ取得完了コールバック
if let url = url {