iPhone Mail App’s Recipient Bubbles

new message screenshot

Notice how the recipient’s names are placed inside a bubble? Well, Apple wasn’t nice enough to provide this functionality out of the box. The three20 lib has a control that will provide this for you as part of the TTMessageController but good luck trying to get compatible up-to-date MonoTouch bindings for it.

So just do it  yourself! All you need is a class that inherits from UIView, let’s call it RecipientBubble. In that class you just need two subviews: a UILabel for the actual text that goes inside and a UIImageView for the background.

The only other thing you’ll need is the background image. How about this one? 

And now the code. Here’s the short version. For the more complete version see the gist on github.

public class RecipientBubble : UIView
  public const string IMAGES_PATH = "Content/Images";
  private static UIImage _backgroundImage;

  private UILabel _titleLabel;
  private UIImageView _backgroundView;

  static RecipientBubble()
    _backgroundImage = UIImage.FromFile("recipientbackground.png").StretchableImage(11, 0);

  public RecipientBubble(string title)
    _titleLabel = new UILabel(new RectangleF(0, -100, 10, 23));
    _titleLabel.Font = UIFont.SystemFontOfSize(14);
    _titleLabel.TextAlignment = UITextAlignment.Center;
    _titleLabel.Text = title;
    _titleLabel.Frame = new RectangleF(0, 0, _titleLabel.Frame.Width + 20, 21);
    _titleLabel.TextColor = UIColor.White;
    _titleLabel.BackgroundColor = UIColor.Clear;

    _backgroundView = new UIImageView(_backgroundImage);
    _backgroundView.Frame = new RectangleF(0, 0, _titleLabel.Frame.Width, 23);
    InsertSubviewBelow(_backgroundView, _titleLabel);


Your end result will look something like this.


ZipArchive for MonoTouch

Compressing files in MonoTouch just got easier!

ZipArchive is taken from the Obj-C world. And it is by far one of the simplest, most elegant solutions I’ve seen for compressing files to an archive. Just open an archive, add files to compress and close it.

var zipFile = new LibZipArchive.ZipArchive();
zipFile.AddFile("/path/of/file/to/somefile.txt", "somefile.txt");
// add more files as needed

The Obj-C library and bindings to C# can be found in the open source project monotouch-ziparchive on github.

Monotouch CLLocationManager AuthorizationStatus

The current release of MonoTouch (3.2.4) does not have bindings for CLLocationManager.AuthorizationStatus. Here is what you can use to get that value until they create that binding.

public static CLAuthorizationStatus AuthorizationStatus
    var classHandle = (new Class("CLLocationManager")).Handle;
    var selectorHandle = new Selector("authorizationStatus").Handle;
    return (CLAuthorizationStatus)Messaging
      .int_objc_msgSend(classHandle, selectorHandle);

Modifying UIScrollView height when keyboard appears

When the keyboard appears over a UIScrollView (that includes the UITableView) the content underneath the keyboard isn’t accessible unless you change the height of the frame of the UIScrollView. Here’s how to do that in MonoTouch.

private NSObject _keyboardShowObserver;
private NSObject _keyboardHideObserver;

// this method assumes the height of a navigation bar on top
// hence the 44 pixels
public override void ViewWillAppear (bool animate)
  base.ViewWillAppear (animate);
  _keyboardShowObserver = NSNotificationCenter.DefaultCenter
    .AddObserver(UIKeyboard.WillShowNotification, (notification) => {
      var keyboardBounds = (NSValue)notification
      var keyboardSize = keyboardBounds.RectangleFValue;
      _scrollView.Frame = new RectangleF(0, 44, View.Frame.Width, 
        View.Frame.Height - keyboardSize.Height - 44);

  _keyboardHideObserver = NSNotificationCenter.DefaultCenter
    .AddObserver(UIKeyboard.WillHideNotification, (notification) => {
      _scrollView.Frame = new RectangleF(0, 44, 
        View.Frame.Width, View.Frame.Height - 44);

// and don't forget to remove the observers on disappear
public override void ViewWillDisappear(bool animate)

Generating Address Book Contacts on iPhone Simulator with MonoTouch

One of the things that I needed for testing my app was a list of contacts to play around with in the Contacts app on the iPhone simulator. The only way to do this is to either manually input contacts — which I am not willing to do for 50+ contacts — or programmatically add them in with a bit of random data.

I have done the latter. Check out the monotouch-generate-contacts project on github. This app, once run on the simulator, will add 50 contacts into the address book and generate some random phone numbers for each contact.

For those of you that don’t care about the project on git hub, here’s the main gist of how you add a contact to the address book.

var addressBook = new ABAddressBook();
var record = new ABPerson();
record.FirstName = "Marco";
record.LastName = "Polo";
var phones = new ABMutableStringMultiValue();
phones.Add("4566545555", ABPersonPhoneLabel.Mobile);