Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.