Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created November 9, 2011 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/1353522 to your computer and use it in GitHub Desktop.
Save Shilo/1353522 to your computer and use it in GitHub Desktop.
A simple category for Sparrow that allows one to auto size an SPTextField.
//
// SPTextField+AutoSize.h
// Sparrow
//
// Created by Shilo White on 9/17/11.
// Copyright 2011 Shilocity Productions. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SPTextField (AutoSize)
- (SPRectangle *)textBounds;
- (void)autoSizeBounds;
- (void)autoSizeWidth;
- (void)autoSizeHeight;
@end
//
// SPTextField+AutoSize.m
// Sparrow
//
// Created by Shilo White on 9/17/11.
// Copyright 2011 Shilocity Productions. All rights reserved.
//
#import "SPTextField+AutoSize.h"
@implementation SPTextField (AutoSize)
- (SPRectangle *)textBounds {
CGSize textSize = [self.text sizeWithFont:[UIFont fontWithName:self.fontName size:self.fontSize] constrainedToSize:CGSizeMake(self.width, self.height) lineBreakMode:UILineBreakModeTailTruncation];
return [SPRectangle rectangleWithX:0 y:0 width:textSize.width height:textSize.height];
}
- (void)autoSizeBounds {
SPRectangle *textBounds = [self textBounds];
self.width = textBounds.width;
self.height = textBounds.height;
}
- (void)autoSizeWidth {
SPRectangle *textBounds = [self textBounds];
self.width = textBounds.width;
}
- (void)autoSizeHeight {
SPRectangle *textBounds = [self textBounds];
self.height = textBounds.height;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment