iphone – 如何向UITextView添加行号?

iphone – 如何向UITextView添加行号?,第1张

概述我想为我的UITextView添加行号. 我必须写我自己的UI-Element,还是有其他解决方案? 我通过子类化UIView并覆盖drawRect:方法来实现这一点: #define TXT_VIEW_INSETS 8.0 // The default insets for a UITextView is 8.0 on all sides@implementation NumberedTex 我想为我的UITextVIEw添加行号.
我必须写我自己的UI-Element,还是有其他解决方案?解决方法 我通过子类化UIVIEw并覆盖drawRect:方法来实现这一点:

#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添加行号?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085590.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存