`
修博龙泉
  • 浏览: 312761 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ios文本框随键盘提升

 
阅读更多
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>{
UIScrollView * _scrollView;
    IBOutlet UITextField *message;
    IBOutlet UIScrollView *myScrollView;
}
@property (retain, nonatomic) IBOutlet UITextField *message;
@property (retain, nonatomic) IBOutlet UIScrollView *myScrollView;
@end


//
//  ViewController.m
//  ChatRoom007
//
//  Created by Bo Xiu on 12-9-14.
//  Copyright (c) 2012年 Bo Xiu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myScrollView;
@synthesize message;
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *myView =  [self bubbleView:@"hello" from:NO];
    [self.view addSubview:myView];
    message.delegate = self;
}

//该方法为点击输入文本框要开始输入是调用的代理方法:就是把view上移到能看见文本框的地方
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGFloat keyboardHeight = 216.0f;
    if (self.view.frame.size.height - keyboardHeight <= textField.frame.origin.y + textField.frame.size.height) {
        CGFloat y = textField.frame.origin.y - (self.view.frame.size.height - keyboardHeight - textField.frame.size.height - 5);
        [UIView beginAnimations:@"srcollView" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.275f];
        self.view.frame = CGRectMake(self.view.frame.origin.x, -y, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

//该方法为点击虚拟键盘Return,要调用的代理方法:隐藏虚拟键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

//该方法为完成输入后要调用的代理方法:虚拟键盘隐藏后,要恢复到之前的文本框地方
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView beginAnimations:@"srcollView" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.275f];
    self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

#pragma mark Table view methods
- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {
	// build single chat bubble cell with given text
	UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
	returnView.backgroundColor = [UIColor clearColor];
    
	UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"bubbleSelf":@"bubble" ofType:@"png"]];
	UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
    
	UIFont *font = [UIFont systemFontOfSize:12];
	CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
    
	UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width+10, size.height+10)];
	bubbleText.backgroundColor = [UIColor clearColor];
	bubbleText.font = font;
	bubbleText.numberOfLines = 0;
	bubbleText.lineBreakMode = UILineBreakModeCharacterWrap;
	bubbleText.text = text;
	
	bubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height+40.0f);
	if(fromSelf)
		returnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height+50.0f);
	else
		returnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height+50.0f);
	
	[returnView addSubview:bubbleImageView];
	[bubbleImageView release];
	[returnView addSubview:bubbleText];
	[bubbleText release];
    
	return [returnView autorelease];
}

- (void)viewDidUnload
{
    [message release];
    message = nil;
    [myScrollView release];
    myScrollView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (void)dealloc {
    [message release];
    [myScrollView release];
    [super dealloc];
}
@end

分享到:
评论

相关推荐

    ios文本框的自动提示

    点击文本框根据输入的文字匹配相应的内容,你可以在回调方法里自己手动添加提示内容,可以支持文字和图片的提示。

    iOS 文本框弹出时间选择器

    iOS 文本框弹出时间选择器,自定义View,兼容多个版本

    iOS为文本框定制二级键盘

    代码演示了如何为UITextField(文本框)定制二级键盘,添加了键盘的展开和折叠功能。

    IOS 解决输入框被键盘遮挡问题

    当键盘高度高于输入框的时候,界面向上拉 从而解决输入框遮挡问题

    IOS自定义随机键盘

    和IOS原生键盘基本一致,可以自定义键盘上的文字,对于一些什么偏远地区的语言IOS原来没有的,可以自定义文字。另外加了键盘的随机分布,只需要在初始化的时候设置isRandomKeyBoard属性,即可每次触发键盘时,键盘...

    ios-一个安全密码文本框和随机键盘.zip

    github地址:... 一个简单的自定义密码文本框和随机键盘,可适用于纯代码,xib,storyboard等情况. 可以使用Pod install platform :ios, '7.0' target :'SafeframeDemo' do pod 'Safeframe' end

    iOS 自定义输入键盘

    iOS 自定义输入键盘

    vue 解决文本框被键盘遮住的问题

    今天小编就为大家分享一篇vue 解决文本框被键盘遮住的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    iOS开发数字键盘,定制键盘

    ios开发中定制个性键盘,数字键盘 定制return按钮,银行卡密码键盘 很有用

    ios定制数字键盘

    ios定制数字键盘,输入身份证

    iOS自定义键盘随机排列

    iOS随机排列0-9数字,因为面试时遇到如何实现随机键盘,提供一个思路供参考

    iOS自定义表情键盘

    iOS自定义表情键盘,根据需要自由切换系统自带和自定义键盘。自定义键盘下方的工具条。

    ios完全自定义键盘

    在网上找了好久没有找到左边是字母右边是数字的那种自定义键盘,于是自己就动手写了一个,由于自定义的键盘没有输入法,不能输入汉字,所以又在自定义键盘上加了一个“系统键盘”,输入汉字的时候点击这个就可以调用...

    ios 开发关闭键盘

    在 iOS 程序中当想要在文本框中输入数据,轻触文本框会打开键盘。对于 iPad 程序,其键盘有一个按钮可以用来关闭键盘,但是 iPhone 程序中的键盘却没有这样的按钮,不过我们可以采取一些方法关闭它。例如,我们可以...

    ios 支付自定义数字键盘

    ios 自定义数字键盘,使用objective-c 开发,适用自定义支付键盘,其他基本功能均可在此基础上进行扩展,如自定义键盘背景图等等;内附使用说明

    ios自定义键盘附件关闭键盘

    ios自定义键盘附件关闭键盘,objective-c语言

    iOS自带回收键盘的UITextField

    对UITextField的类的扩展。 很好用的键盘扩展及键盘回收的使用 只要调用一句话方法 键盘随心所以

    iOS 获取系键盘

    系统键盘、第三方键盘完全兼容 1、键盘高度 2、键盘速度

    iOS-聊天键盘封装

    一款在iOS平台上使用的聊天键盘,支持emoji表情,大表情,语音,更多功能。

    ios-车牌键盘.zip

    一款支持新能源、领事馆、机场专牌、武警专牌等多项特殊格式的车牌输入键盘。 有疑问欢迎来github上来问,如果有帮助也欢迎star。地址https://github.com/parkingwang/vehicle-keyboard-ios

Global site tag (gtag.js) - Google Analytics