Skip to content

Instantly share code, notes, and snippets.

@Ridwy
Created April 28, 2014 04:49
Show Gist options
  • Save Ridwy/11362038 to your computer and use it in GitHub Desktop.
Save Ridwy/11362038 to your computer and use it in GitHub Desktop.
デバッガみたいに自動でスクロールアップするUITextView
@interface SomeViewController ()
@property (weak) UITextView *logView;
@end
@implementation SomeViewController
- (void)writeOSDLog:(NSString *)log
{
if (self.logView == nil) {
NSTextStorage *textStorage = [NSTextStorage new];
NSLayoutManager *layoutManager = [NSLayoutManager new];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[textStorage addLayoutManager:layoutManager];
[layoutManager addTextContainer:textContainer];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(8, 200, 300, 200)
textContainer:textContainer];
textView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
textView.textColor = [UIColor greenColor];
textView.font = [UIFont systemFontOfSize:9];
textView.editable = NO;
[self.view addSubview:textView];
self.logView = textView;
}
NSDateFormatter *logDateFormatter = [[NSDateFormatter alloc] init];
logDateFormatter.dateFormat = @"HH:mm:ss.SS";
NSString *string = [self.logView.text stringByAppendingFormat:@"%@ %@\n",
[logDateFormatter stringFromDate:[NSDate date]], log];
if (3000 < string.length) {
string = [string substringFromIndex:string.length - 3000];
}
self.logView.text = string;
[self.logView scrollRangeToVisible:NSMakeRange(string.length, 0)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment