大佬教程收集整理的这篇文章主要介绍了iphone – 如何向UITextView添加行号?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#define TXT_VIEW_INSETS 8.0 // The default insets for a UITextView is 8.0 on all sides @implementation numberedTextView @synthesize linenumbers; @synthesize delegate; - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self setContentMode:UIViewContentModeRedraw]; internalScrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; [internalScrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [internalScrollView setBACkgroundColor:[UIColor clearColor]]; [internalScrollView setClipsToBounds:YES]; [internalScrollView setScrollsToTop:YES]; [internalScrollView setContentSize:self.bounds.size]; [internalScrollView setContentMode:UIViewContentModeLeft]; [internalScrollView setDelegate:self]; [internalScrollView setBounces:NO]; internalTextView = [[UITextView alloc] initWithFrame:self.bounds]; [internalTextView setAutocapitalizationType:UITextAutocapitalizationTypeNone]; [internalTextView setAutocorrectionType:UITextAutocorrectionTypeNo]; [internalTextView setSpellcheckingType:UITextSpellcheckingTypeNo]; [internalTextView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; [internalTextView setBACkgroundColor:[UIColor clearColor]]; [internalTextView setClipsToBounds:YES]; [internalTextView setScrollsToTop:NO]; [internalTextView setContentMode:UIViewContentModeLeft]; [internalTextView setDelegate:self]; [internalTextView setBounces:NO]; [internalScrollView addSubview:internalTextView]; [self addSubview:internalScrollView]; } return self; } - (void)drawRect:(CGRect)rect { if (self.linenumbers) { [[internalTextView textColor] set]; CGFloat xOrigin,yOrigin,width/*,height*/; uint numberOfLines = (internalTextView.contentSize.height + internalScrollView.contentSize.height) / internalTextView.font.lineHeight; for (uint x = 0; x < numberOfLines; ++X) { NSString *lineNum = [NSString StringWithFormat:@"%d:",x]; xOrigin = CGRectGetMinX(self.bounds); yOrigin = ((internalTextView.font.pointSize + abs(internalTextView.font.descender)) * X) + TXT_VIEW_INSETS - internalScrollView.contentOffset.y; width = [lineNum sizeWithFont:internalTextView.font].width; // height = internalTextView.font.lineHeight; [lineNum drawAtPoint:CGPointMake(xOrigin,yOrigin) withFont:internalTextView.font]; } CGRect tvFrame = [internalTextView frame]; tvFrame.size.width = CGRectGetWidth(internalScrollView.bounds) - width; tvFrame.size.height = MAX(internalTextView.contentSize.height,CGRectGetHeight(internalScrollView.bounds)); tvFrame.origin.x = width; [internalTextView setFrame:tvFrame]; tvFrame.size.height -= TXT_VIEW_INSETS; // This fixed a weird content size problem that I've forgotten the specifics of. [internalScrollView setContentSize:tvFrame.size]; } } #pragma mark - UITextView Delegate - (BOOL)textView:(UITextView *)textView shouldChangeTexTinRange:(NSRangE)range replacementText:(NSString *)text { [self setNeedsDisplay]; return YES; } - (void)textViewDidChange:(UITextView *)textView { [self setNeedsDisplay]; } - (void)textViewDidChangeSELEction:(UITextView *)textView { [self setNeedsDisplay]; } #pragma mark - UIScrollView Delegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self setNeedsDisplay]; }
以上是大佬教程为你收集整理的iphone – 如何向UITextView添加行号?全部内容,希望文章能够帮你解决iphone – 如何向UITextView添加行号?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。