Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSAttributedStringのboundingRectWithSize:options:attributes:context:を使って表示テキストにあわせてサイズを調整します。 (iOS6以上) (http://www.toyship.org/archives/1437)
- (void)viewDidLoad
{
[super viewDidLoad];
//boundingRectWithSize:options:contextを使う
NSDictionary *attributes = @{ NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:12.0f] };
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Every great iOS app starts with a great idea, but translating that idea into actions requires some planning. Every iOS app relies heavily on design patterns, and those design patterns influence much of the code you need to write. So before you write any code, take the time to explore the possible techniques and technologies available for writing that code. Doing so can save you a lot of time and frustration."
attributes:attributes];
UILabel* myLabel = [[UILabel alloc] init];
myLabel.backgroundColor = [UIColor blueColor];
myLabel.numberOfLines = 0;
myLabel.attributedText = string;
CGRect frame1 = [string boundingRectWithSize:CGSizeMake(200, 900) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
frame1.origin.y = 100;
myLabel.frame = frame1;
[self.view addSubview:myLabel];
UITextView* myTextView = [[UITextView alloc] init];
myTextView.backgroundColor = [UIColor redColor];
myTextView.editable = NO;
myTextView.attributedText = string;
CGRect frame2 = [string boundingRectWithSize:CGSizeMake(180, 900) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
frame2.size.width = 200;
frame2.origin.y = 300;
myTextView.frame = frame2;
[self.view addSubview:myTextView];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment