Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
dannycabrera / Log.cs
Created March 24, 2017 19:10
NSLog workaround for Xamarin Console.WriteLine bug
const string FoundationLibrary = "/System/Library/Frameworks/Foundation.framework/Foundation";
[System.Runtime.InteropServices.DllImport(FoundationLibrary)]
extern static void NSLog(IntPtr format, IntPtr s);
[System.Runtime.InteropServices.DllImport(FoundationLibrary, EntryPoint = "NSLog")]
extern static void NSLog_ARM64(IntPtr format, IntPtr p2, IntPtr p3, IntPtr p4, IntPtr p5, IntPtr p6, IntPtr p7, IntPtr p8, IntPtr s);
static readonly bool Is64Bit = IntPtr.Size == 8;
static readonly bool IsDevice = ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE;
@dannycabrera
dannycabrera / Random.cs
Last active December 30, 2016 17:09
Random pastel color generator
using System;
using UIKit;
// based on http://blog.functionalfun.net/2008/07/random-pastel-colour-generator.html
public static UIColor RandomColor ()
{
Random random = new Random ();
// to create lighter colors:
// take a random integer between 0 & 128 (rather than between 0 and 255)
@dannycabrera
dannycabrera / LevenshteinDistance.cs
Created December 16, 2016 16:05
LevenshteinDistance computation
using System;
/// <summary>
/// Contains approximate string matching
/// </summary>
static class LevenshteinDistance
{
/// <summary>
/// Compute the distance between two strings.
/// </summary>
@dannycabrera
dannycabrera / AirPrint.cs
Created November 22, 2016 21:00
Printing PDF with UIKit.UIPrint on Xamarin.iOS
using UIKit;
void AirPrint (CGRect frame, string pdfOrImageToPrint)
{
var printer = UIPrintInteractionController.SharedPrintController;
if (printer == null) {
Console.WriteLine("Unable to print at this time.");
} else {
var printInfo = UIPrintInfo.PrintInfo;
@dannycabrera
dannycabrera / AppDelegate.cs
Created November 2, 2016 12:51
Get notified when user takes screenshot on Xamarin.iOS
using Foundation;
using UIKit;
namespace YourApp
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
NSObject _screenshotNotification = null;
@dannycabrera
dannycabrera / SaveRTFFromUITextView.cs
Last active October 27, 2016 13:38
Save RTF from UITextView using NSFileWrapper on Xamarin.iOS
NSError error1 = null;
NSError error2 = null;
var atts = new NSAttributedStringDocumentAttributes ();
atts.DocumentType = NSDocumentType.RTF;
using (var wrapper = textView.AttributedText.GetFileWrapperFromRange (new NSRange (0, textView.Text.Length), atts, ref error1))
{
wrapper.Write (new NSUrl ("...Edited.rtf", false), NSFileWrapperWritingOptions.Atomic, new NSUrl ("...Sample.rtf", false), out error2);
}
@dannycabrera
dannycabrera / LoadUITextViewWithRTF.cs
Created October 27, 2016 13:32
Load RTF document into UITextView using Xamarin.iOS
NSError error = null;
var attributedString = new NSAttributedString (new NSUrl ("...Sample.rtf", false), null, ref error);
var attributedTextHolder = new NSMutableAttributedString (attributedString);
textView.AllowsEditingTextAttributes = true;
textView.AttributedText = attributedTextHolder;
@dannycabrera
dannycabrera / GetVideoThumbnail.cs
Created March 1, 2016 15:49
Xamarin.iOS get video thumbnail
private UIImage GetVideoThumbnail(string path)
{
try {
CMTime actualTime;
NSError outError;
using (var asset = AVAsset.FromUrl (NSUrl.FromFilename (path)))
using (var imageGen = new AVAssetImageGenerator (asset))
using (var imageRef = imageGen.CopyCGImageAtTime (new CMTime (1, 1), out actualTime, out outError)) {
return UIImage.FromImage (imageRef);
}
@dannycabrera
dannycabrera / AppDelegate.cs
Created December 30, 2015 22:20
Xamarin.iOS Blur screen with OnResignActivation
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIVisualEffectView _blurView = null;
public override void OnActivated (UIApplication application)
{
try {
if (_blurView != null) {
_blurView.RemoveFromSuperview ();
@dannycabrera
dannycabrera / gist:91360a81c182cfa30dac
Created December 2, 2014 02:08
Register for iOS 8 Notifications
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet (new string[] {}));
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
return true;
}