将文本输入边界限制在 iOS
Limit the text input boundaries on iOS
我正在为我的公司开发一个应用程序,但我不是 obj-C 编程语言方面的专家。我试图在互联网上找到一些答案,但根本没有成功。问题的解决方法可能很简单,但我无法解决。
我想限制文本使其适合文本框。现在正在发生的事情是,当我开始打字时,它永远在第一行流动,并且当它到达框边界时不会改变下降到第二行。
otherdetails = [[UITextField alloc] initWithFrame:otherdetailsf];
otherdetails.text = otherdetailstxt;
[otherdetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherdetails.layer setBorderWidth:2.0];
otherdetails.layer.cornerRadius = 5;
otherdetails.clipsToBounds = YES;
otherdetails.delegate = self;
otherdetails.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
otherdetails.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
这就是我在文本字段中键入大量内容时发生的情况....
我该如何解决这个问题?
谢谢!!
如果您需要在文本到达行尾时支持多行文本,请使用 TextView
而不是文本字段。
来自文档:
The UITextView class implements the behavior for a scrollable,
multiline text region. The class supports the display of text using
custom style information and also supports text editing. You typically
use a text view to display multiple lines of text, such as when
displaying the body of a large text document.
所以你的代码应该是:
otherDetails=[[UITextView alloc]initWithFrame:otherdetailsf];
otherDetails.text=@"";
[otherDetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherDetails.layer setBorderWidth:2.0];
otherDetails.layer.cornerRadius = 5;
otherDetails.clipsToBounds = YES;
otherDetails.delegate = self;
// otherDetails.contentMode=UIControlContentVerticalAlignmentTop;
试试这个代码:
在viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextViewDelegate>
@property (strong,nonatomic)IBOutlet UITextView *mytextview;
@end
在viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mytextview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTextView
{
[mytextview setText:@"Lorem ipsum dolor sit er elit lamet"];
mytextview.delegate = self;
[self.view addSubview:mytextview];
mytextview.delegate = self;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;}
-(void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"Did begin editing");
}
-(void)textViewDidChange:(UITextView *)textView{
NSLog(@"Did Change");
}
-(void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"Did End editing");
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
@end
我正在为我的公司开发一个应用程序,但我不是 obj-C 编程语言方面的专家。我试图在互联网上找到一些答案,但根本没有成功。问题的解决方法可能很简单,但我无法解决。
我想限制文本使其适合文本框。现在正在发生的事情是,当我开始打字时,它永远在第一行流动,并且当它到达框边界时不会改变下降到第二行。
otherdetails = [[UITextField alloc] initWithFrame:otherdetailsf];
otherdetails.text = otherdetailstxt;
[otherdetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherdetails.layer setBorderWidth:2.0];
otherdetails.layer.cornerRadius = 5;
otherdetails.clipsToBounds = YES;
otherdetails.delegate = self;
otherdetails.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
otherdetails.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
这就是我在文本字段中键入大量内容时发生的情况....
我该如何解决这个问题?
谢谢!!
如果您需要在文本到达行尾时支持多行文本,请使用 TextView
而不是文本字段。
来自文档:
The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.
所以你的代码应该是:
otherDetails=[[UITextView alloc]initWithFrame:otherdetailsf];
otherDetails.text=@"";
[otherDetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherDetails.layer setBorderWidth:2.0];
otherDetails.layer.cornerRadius = 5;
otherDetails.clipsToBounds = YES;
otherDetails.delegate = self;
// otherDetails.contentMode=UIControlContentVerticalAlignmentTop;
试试这个代码:
在viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextViewDelegate>
@property (strong,nonatomic)IBOutlet UITextView *mytextview;
@end
在viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mytextview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTextView
{
[mytextview setText:@"Lorem ipsum dolor sit er elit lamet"];
mytextview.delegate = self;
[self.view addSubview:mytextview];
mytextview.delegate = self;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;}
-(void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"Did begin editing");
}
-(void)textViewDidChange:(UITextView *)textView{
NSLog(@"Did Change");
}
-(void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"Did End editing");
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
@end