如何让UITextView文字垂直居中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何让UITextView文字垂直居中相关的知识,希望对你有一定的参考价值。
UITextView的居中属性,只能使文字顶部居中,不能使内容垂直居中,达不到UITextFiled的居中的效果;因此想要使 UITextVIew的内容垂直居中,可以通过KVC的方式监听text或者contentSize属性,进行偏移。
代码如下:
//注册 监听text属性的事件
//接收处理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context
UITextView *mTrasView = object;
CGFloat topCorrect = ([mTextbounds].size.height - [mTextcontentSize].height);
topCorrect = (topCorrect <0.0 ?0.0 : topCorrect);
mText.contentOffset = (CGPoint).x =0, .y = -topCorrect/2;
当然,别忘了先设置一下水平居中属性。
//
//如何做到输入时动态改变:也许可以在
- (void)textViewDidChange:(UITextView *)textView;进行上述的偏移处理,同时可以加上动画效果,以便达到UITextFiled,居中输入的动态效果,此处只提供一个思路,其方法进行过验证,可行。但细节上需要你自己根据情况改变。
参考技术A 让UITextView和UITextField同样拥有垂直居中的属性,建议单独一个类继承自UITextView只需要初始化UITextView之后用KVO监听 "contentSize" 属性即可
_textView = [[UITextView alloc] initWithFrame:CGRectMake(20.0f, DOT_COORDINATE,260.0f, TABLE_VIEW_ROW_HEIGHT*2)];
_textView.delegate = self;
_textView.text = PROMPT_TEXT;
_textView.font = [UIFont systemFontOfSize:18.0f];
_textView.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:_textView];
[_textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
#pragma mark - KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
UITextView *tv = object;
// Center vertical alignment
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tvzoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
tv.contentOffset = (CGPoint).x = 0, .y = -topCorrect;
// // Bottom vertical alignment
// CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
// topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
// tv.contentOffset = (CGPoint).x = 0, .y = -topCorrect;
参考技术B TextView.setGravity(Gravity.CENTER);//居中
TextView.setGravity(Gravity.CENTER_HORIZONTAL);//水平居中
TextView.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
Delphi7如何实现让Tedit显示文字垂直居中(上下居中)
通过下面的组件,可以在输入文字的时候自动垂直居中
直接把下面代码保存到Unit1.pas即可
------------------------------------------
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TEdit = class(StdCtrls.TEdit) protected procedure CreateParams(var Params: TCreateParams); override; procedure KeyPress(var Key: Char); override; procedure WMSize(var msg: TWMSize);message WM_SIZE; procedure SetParent(AParent: TWinControl);override; procedure SetCenter; end; TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } Edt: TEdit; end; var Form1: TForm1; implementation {$R *.dfm} { TEdit } procedure TForm1.FormCreate(Sender: TObject); begin Edt := TEdit.Create(self); Edt.Parent := self; Edt.AutoSize := False; Edt.Height := 50; end; procedure TEdit.CreateParams(var Params: TCreateParams); begin inherited; Params.Style := Params.Style or ES_MULTILINE; end; procedure TEdit.KeyPress(var Key: Char); begin inherited; if Key = #13 then key := #0; end; procedure TEdit.WMSize(var msg: TWMSize); begin inherited; SetCenter; end; procedure TEdit.SetParent(AParent: TWinControl); begin inherited; if Parent <> nil then begin SetCenter; end; end; procedure TEdit.SetCenter; var DC: HDC; SaveFont: HFont; Sin: Integer; SysMetrics, Metrics: TTextMetric; Rct: TRect; begin DC := GetDC(0); GetTextMetrics(DC, SysMetrics); SaveFont := SelectObject(DC, Font.Handle); GetTextMetrics(DC, Metrics); SelectObject(DC, SaveFont); ReleaseDC(0, DC); if Ctl3D then Sin := 8 else Sin := 6; Rct := ClientRect; Sin := Height - Metrics.tmHeight - Sin; Rct.Top := Sin div 2; SendMessage(Handle, EM_SETRECT, 0, Integer(@Rct)); end; end.
当这个保存成unit1.pas 后,然后通过delphi组件安装功能来安装组件,具体安装方法可以到网上查方法
以上是关于如何让UITextView文字垂直居中的主要内容,如果未能解决你的问题,请参考以下文章