只 一行显示可左右滚动的文本(UITextField中文限制)
2015-05-08 20:54
387 查看
// // ViewController.m // 一行显示可滚动的文本 // // Created by apple on 15-5-8. // Copyright (c) 2015年 apple. All rights reserved. // #import "ViewController.h" @interface ViewController () { UIScrollView *_scrollView; UITextField *_textField1; NSInteger _indextext; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(70,CGRectGetHeight(self.view.bounds)/2,CGRectGetWidth(self.view.bounds) - 100, 30)]; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.bounces = NO; _scrollView.layer.borderWidth = 1; [self.view addSubview:_scrollView]; NSMutableAttributedString * attributedStr4 = [[NSMutableAttributedStringalloc]initWithString:@"请输入约会内容"]; [attributedStr4 addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, attributedStr4.length)]; _textField1 = [[UITextField alloc]initWithFrame:CGRectMake(0,7,CGRectGetWidth(self.view.bounds) - 100, 15)]; _textField1.textAlignment = NSTextAlignmentRight; _textField1.text = @""; _textField1.textColor = [UIColor grayColor]; _textField1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13]; _textField1.attributedPlaceholder = attributedStr4; [_textField1 addTarget:self action:@selector(textFieldEditChanged:)forControlEvents:UIControlEventEditingChanged]; [_scrollView addSubview:_textField1]; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_textField1.bounds),30); } - (void)textFieldEditChanged:(UITextField *)textField { if (CGRectGetWidth(_textField1.bounds) >=CGRectGetWidth(self.view.bounds) - 100) { _scrollView.contentSize =CGSizeMake(CGRectGetWidth(_textField1.bounds),30); NSLog(@"%.2f",_scrollView.contentSize.width); if (_indextext != _textField1.text.length) { [_textField1 sizeToFit]; [_scrollViewsetContentOffset:CGPointMake(CGRectGetWidth(_textField1.bounds) -CGRectGetWidth(_scrollView.bounds), 0) animated:NO]; _indextext = _textField1.text.length; } } else { _textField1.frame = CGRectMake(0, 7,CGRectGetWidth(self.view.bounds) - 100, 15); [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO]; _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds) - 100,30); _indextext = _textField1.text.length; } if (_textField1.text.length > 36) { _textField1.text = [_textField1.text substringToIndex:36]; } } @end
补1:我这方法其实很笨,大家有好的方法共享出来,共同进步!
补2:textfield加个状态监听器-限制中文
- (void)viewDidLoad { [_textField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged]; [super viewDidLoad]; } - (void)textFieldEditChanged:(UITextField *)textField { NSLog(@"textField text : %@", [textField text]); }
这样,无论是字母还是中文,都能动态获取
上面的中文限制是有bug的当你一直输入拼音不选择汉子到一定数量汉字联想自动关闭了
修改后链接:
/article/5254421.html
相关文章推荐
- GridView实现一行显示并左右滚动
- GridView实现一行显示并左右滚动
- 一行代码,让 UITextField 文本以密码方式显示
- [iOS] UITextField如何准确限制中文输入文本长度
- Android滚动显示TXT中文文本
- Android GridView 一行显示数据(包括图片和文本),解决的办法是计算数据占该行的宽度是多少
- android 实现在文本内容超过固定宽度可手动左右滚动查看效果
- Android中TextView限制一行显示及其它效果
- C#Textbox滚动显示最后一行,不闪烁
- 完美解决 向UILable 文字最后插入N张图片,支持向限制行数的UILable 最后一行插入,多余文字显示...
- //随记。关于存在换行符 \n 的文本限制 行数(在设置内容之前就知道有几行)收起展开是否需要显示 小记
- iOS UITextField 限制只能输入中文
- 超出文本显示省略号(包括一行和多行)
- listView始终显示新添加的一行,滚动条自动向下滚动
- VC6.0在文本编辑区中文显示乱码解决办法
- 文本输入框限制输入:12个字符或6个中文(中英文夹杂)
- superSlide显示左右滚动
- TextView单行显示,文字左右滚动(走马灯效果)实现条件:
- 读取中文文本,将每个字存入向量,然后显示出来
- IOS:UITextField输入中文时限制长度