简体   繁体   English

UITextView 文本垂直对齐

[英]UITextView text vertical Alignment

I have created Custom UITextView class ,where textView grows dynamically as user types characters and textview frame resized dynamically.I want to align the text vertically center in the frame,it always coming to bottom by default.I added UIEdgeInsets to make or look a like it into center but it creates more issues for me.If i try to set content offset then the textView center also shifts.Is the content offset and textView center effect each other ?.I want textview center where ever it is but the text inside the textView stays vertically center.我创建了自定义UITextView类,其中 textView 随着用户键入字符而动态增长,并且 textview 框架动态调整大小。我想在框架中垂直居中对齐文本, UIEdgeInsets下它总是到底部。我添加了UIEdgeInsets来制作或看起来像它进入中心,但它给我带来了更多问题。如果我尝试设置内容偏移,那么 textView 中心也会移动。内容偏移和 textView 中心是否相互影响?. textView 保持垂直居中。 Initially i am setting UITextView font 23.0 with a rectangular border(textview frame layer border).When i start typing in that box the text is coming on bottom of the border.Then i am using最初我设置 UITextView 字体 23.0 带有矩形边框(textview 框架层边框)。当我开始在那个框中输入时,文本出现在边框的底部。然后我使用

[textView setContentInset:UIEdgeInsetsMake(5,0,10,0)]; 

to make text in center.使文本居中。 Then i have to send this text for printing which is placed on an UIImageView.Here i am using the mentioned code by Paras to match printer dpi and resizing it.But the text as looks on UIImageview doesn't match the exact placement of print because i have added UIEdgeInset to adjust vertical alignment然后我必须发送此文本以进行打印,该文本放置在 UIImageView 上。这里我使用 Paras 提到的代码来匹配打印机 dpi 并调整其大小。但是 UIImageview 上的文本与打印的确切位置不匹配,因为我添加了 UIEdgeInset 来调整垂直对齐

Use this category to UITextView Class: https://gist.github.com/toms972/8330377 将此类别用于UITextView类: https//gist.github.com/toms972/8330377

OR 要么

Follow the steps described in the following link: http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/ 请按照以下链接中描述的步骤操作: http//imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/

you can try this sample https://github.com/HansPinckaers/GrowingTextView 你可以尝试这个示例https://github.com/HansPinckaers/GrowingTextView

I hope this would help you. 我希望这会对你有所帮助。

I have two types to set dynamic height to the UITextView see both are bellow... 我有两种类型来设置动态高度到UITextView看到两者都吼...

UPDATE: 更新:

First Create UITextView programmatically like bellow... 首先以编程方式创建UITextView如下所示......

-(IBAction)btnAddTextView:(id)sender
{
    UIView *viewTxt = [[UIView alloc]initWithFrame:CGRectMake(imgBackBoard.center.x - 100,20, 220, 84)];
    [viewTxt setBackgroundColor:[UIColor clearColor]];
    viewTxt.userInteractionEnabled = YES;

    UITextView *txtAddNote=[[UITextView alloc]initWithFrame:CGRectMake(20,20, 180, 44)];
    [txtAddNote setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    [txtAddNote setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
    txtAddNote.layer.borderWidth = 2.0;
    txtAddNote.layer.borderColor= [UIColor redColor].CGColor;
    viewTxt.tag = 111;
    txtAddNote.tag = 111;
    txtAddNote.userInteractionEnabled= YES;
    txtAddNote.delegate = self;
    txtAddNote.textColor = [UIColor whiteColor];
    [viewTxt addSubview:txtAddNote];
    [viewBoard addSubview:viewTxt];
    [txtAddNote release];

}

First 第一

1. This bellow Method is Delegate Method of UITextViewDelegate . 1.本波纹管的方法是委托方法UITextViewDelegate

in .h class add this UITextViewDelegate and then give your textView.delegate to self like bellow.. .h类中添加这个UITextViewDelegate然后将你的textView.delegateself像下面的..

yourTextView.delegate = self;

and use paste this bellow method... 并使用粘贴此波纹管方法...

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);
    return YES;
}

here when your UITextView editing at a time with its text, the content size of textView is change. 这里当您的UITextView编辑其文本时,textView的内容大小会发生变化。

2. Use my bellow custom Method for set Dynamic Height of UILable , UITextField and also UITextView 2.使用我的波纹管自定义方法设置UILableUITextFieldUITextView动态高度

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

use this method like bellow... 使用这种方法如下...

CGSize sizeToMakeLabel = [yourTextView.text sizeWithFont:yourTextView.font]; 
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, yourTextView.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 

I hope this is helpful to you :) 我希望这对你有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM