只 一行顯示可左右滾動的文本(UITextField中文限制),滾動uitextfield

來源:互聯網
上載者:User

只 一行顯示可左右滾動的文本(UITextField中文限制),滾動uitextfield

////  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]);}

這樣,無論是字母還是中文,都能動態擷取

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.