Created
April 28, 2014 04:49
-
-
Save Ridwy/11362038 to your computer and use it in GitHub Desktop.
デバッガみたいに自動でスクロールアップするUITextView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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