textView和textField

来源:互联网 发布:淘宝反渗透膜 编辑:程序博客网 时间:2024/06/03 17:22

在viewDidLoad中实现下面的方法

//实现光标的在最上面显示

    [textView addObserver:selfforKeyPath:@"contentSize"options:NSKeyValueObservingOptionNewcontext:nil];


在viewDidLoad外面实现下面的方法

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    UITextView *textView = object;

    textView.contentOffset = (CGPoint){.x =0,.y = 0};

}


textField


//该方法 放在外面调用

-(void)setPadding:(BOOL)enable top:(float)top right:(float)right bottom:(float)bottom left:(float)left

{

    isEnablePadding = enable;

    paddingTop      = top;

    paddingRight    = right;

    paddingBottom   = bottom;

    paddingLeft     = left;

}

//设置 光标位置

- (CGRect)textRectForBounds:(CGRect)bounds {

    

    if (isEnablePadding) {

        return CGRectMake(bounds.origin.x +paddingLeft,

                          bounds.origin.y +paddingTop,

                          bounds.size.width -paddingRight, bounds.size.height -paddingBottom);

    }

    else{

        return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);

    }

}



#pragma mark //控制placeHolder的颜色、字体

- (void)drawPlaceholderInRect:(CGRect)rect{

    [LOGINANDREGISTER_COLORsetFill];

    [[selfplaceholder]drawInRect:rect withFont:[UIFont systemFontOfSize:NAVIGATION_TITLE_TEXTSIZE]];

}



//控制placeHolder的位置,左右缩20

-(CGRect)placeholderRectForBounds:(CGRect)bounds

{

    //return CGRectInset(bounds, 20, 0);

    CGRect inset ;

    inset = CGRectMake(15,30, 200, bounds.size.height);

    if (IOS_DEVICE>=7) {

    inset = CGRectMake(13,35, 200, bounds.size.height);//”13“光标位置

    }

    return inset;

}



//重写 UITextField右边“X”图标 的位置

- (CGRect)clearButtonRectForBounds:(CGRect)bounds{

    CGRect inset ;

    inset = CGRectMake(self.frame.size.width-20,33, 15, 15);

    if (IOS_DEVICE>=7) {

        inset = CGRectMake(self.frame.size.width-20,31, 15, 15);

    }

    return inset;

}



0 0
原创粉丝点击