ios UITextView的placeHolder的设置
- 格式:doc
- 大小:102.05 KB
- 文档页数:15
UIWebView键盘的accessoryBar隐藏当前越来越多的iOS应用开始使用html进行交互显示,却会发现在UIWebView弹出的键盘比UITextView的键盘多出了左右移动以及完成按钮,那么如何去掉这些我们并不需要的按钮呢?苹果官方并没有给出方法,所以我们只能够自己来解决。
webview_keyboard.png对于不同的iOS版本,我们的处理方法也会不同,因为官方没有提供方法,所有的方法都是需要自己去发现和修改。
这里只写出iOS7以上版本的方法:func hideKeyBoard() -> V oid {for window inUIApplication.sharedApplication().windows {if !window.isMemberOfClass(UIWindow.self) {let keyboardWindow = windowif #available(iOS 9.0, *) {self.removeAccessoryBarForiOS9(keyboardWindow as UIView)} else if #available(iOS 8.0, *) {self.removeAccessoryBarForiOS8(keyboardWindow as UIView)} else {self.removeAccessoryBarForiOS7(keyboardWindow as UIView)}}}if #available(iOS 9.0, *) {self.removeAccessoryBarForiOS9(UIApplication.sharedApplica tion()st! as UIView)}}funcremoveAccessoryBarForiOS9(keyboardWindow:UIView) ->V oid {for possibleFormView:UIView in keyboardWindow.subviews {ifpossibleFormView.isMemberOfClass(NSClassFromString("UII nputSetContainerView")!) {for subviewOfInputSetContainerView in possibleFormView.subviews {if subviewOfInputSetContainerView.isMemberOfClass(NSClassFr omString("UIInputSetHostView")!) {for subviewOfInputSetHostView in subviewOfInputSetContainerView.subviews {// 隐藏工具条NSClassFromStringif subviewOfInputSetHostView.isMemberOfClass(NSClassFromS tring("UIWebFormAccessory")!) {yer.opacity = 0subviewOfInputSetHostView.frame = CGRectZero} else if (subviewOfInputSetHostView.isMemberOfClass(NSClassFrom String("_UIRemoteKeyboardPlaceholderView")!)) {yer.opacity = 0subviewOfInputSetHostView.frame = CGRectZero// 这里使用了私有方法获取对应的accessorBar,然后进行隐藏var accessory = subviewOfInputSetHostView.performSelector(Selector("placehe ldView")).takeRetainedValue()ifaccessory.isMemberOfClass(NSClassFromString("UIWebForm Accessory")!) {let accessory = accessory as! UIViewyer.opacity = 0accessory.frame = CGRectZero}}// 键盘背景,UIKBInputBackdropView有两个只隐藏上面的else if subviewOfInputSetHostView.isMemberOfClass(NSClassFromS tring("UIKBInputBackdropView")!) && subviewOfInputSetHostView.frame.size.height < 100 {yer.opacity = 0erInteractionEnabled = false}}}}}}}funcremoveAccessoryBarForiOS8(keyboardWindow:UIView) -> V oid {for possibleFormView:UIView in keyboardWindow.subviews {ifpossibleFormView.isMemberOfClass(NSClassFromString("UII nputSetContainerView")!) {for subviewOfInputSetContainerView in possibleFormView.subviews {if subviewOfInputSetContainerView.isMemberOfClass(NSClassFr omString("UIInputSetHostView")!) {for subviewOfInputSetHostView in subviewOfInputSetContainerView.subviews {// 隐藏工具条if subviewOfInputSetHostView.isMemberOfClass(NSClassFromS tring("UIWebFormAccessory")!) {yer.opacity = 0subviewOfInputSetHostView.frame = CGRectZero}// 键盘背景, UIKBInputBackdropView有两个只隐藏上面的else ifsubviewOfInputSetHostView.isMemberOfClass(NSClassFromS tring("UIKBInputBackdropView")!) && subviewOfInputSetHostView.frame.size.height < 100 {yer.opacity = 0erInteractionEnabled = false}}}}}}}funcremoveAccessoryBarForiOS7(keyboardWindow:UIView) -> V oid {for possibleFormView:UIView in keyboardWindow.subviews {ifpossibleFormView.isMemberOfClass(NSClassFromString("UIPeripheralHostView")!) {for subviewOfPeripheralHostView in possibleFormView.subviews {// 隐藏工具条if subviewOfPeripheralHostView.isMemberOfClass(NSClassFrom String("UIWebFormAccessory")!) {yer.opacity = 0subviewOfPeripheralHostView.frame = CGRectZero}// 键盘背景, UIKBInputBackdropView有两个只隐藏上面的else if subviewOfPeripheralHostView.isMemberOfClass(NSClassFrom String("UIKBInputBackdropView")!) && subviewOfPeripheralHostView.frame.size.height < 100 {yer.opacity = 0erInteractionEnabled = false}}}}}实际中,隐藏键盘accessory后的样子如下图:webview_keyboard_hideaccessory.png需要注意的是,在示例代码使用设置layer透明,frame为空来进行隐藏,而不使用removeFromSuperView,是因为使用了removeFromSuperView,在键盘重新布局时会导致crash。
iOS开发中的常用控件介绍与使用一、导语iOS开发中,控件是构建界面的基本单位,它们可以让我们更加方便地与用户进行交互。
本文将介绍iOS开发中常用的控件,以及它们的使用方法和注意事项。
二、标签(Label)标签是iOS界面中最简单的控件之一,用于显示一段文本。
我们通常使用UILabel类来创建和管理标签。
通过设置UILabel的属性,我们可以改变标签的字体、颜色、对齐方式等。
三、按钮(Button)按钮是iOS界面中常见的交互控件,用于响应用户的点击事件。
UIButton类可以创建各种样式的按钮,如矩形按钮、圆形按钮等。
我们可以通过UIButton的监听事件,实现按钮点击后的相应操作。
四、文本框(TextField)文本框用于接收用户输入的文本信息。
我们可以使用UITextField类创建一个文本框,并设置其样式和代理。
通过UITextField的代理方法,我们可以监听文本框的各种事件,如开始编辑、结束编辑等。
五、图片视图(ImageView)图片视图用于显示图片资源,可以通过UIImageView类来创建和管理。
我们可以通过设置UIImageView的属性,实现图片的缩放、旋转等效果。
此外,UIImageView还支持从网络加载图片,并提供了缓存功能。
六、滑动视图(ScrollView)滑动视图是一种特殊的容器控件,可以滚动显示其中的内容。
使用UIScrollView类,我们可以创建一个可滚动的视图,并设置其内容大小和滚动范围。
通过UIScrollView的代理方法,我们可以监听滚动事件,实现自定义的滚动效果。
七、表格视图(TableView)表格视图是iOS中常用的列表控件,适用于展示有序的多条数据。
UITableView类可以创建一个表格视图,并设置其数据源和代理。
我们可以通过UITableView的代理方法,实现表格的行数、样式、编辑等自定义操作。
八、选择器(Picker)选择器用于用户在有限的选项中进行选择,常见的使用场景包括日期选择、城市选择等。
iPhone iOS平台视觉规范说明书目录1.创建优秀的用户界面 (7)2.视图VIEW (5)2.1状态栏STATUS BAR (5)2.2导航栏NAVIGATION BAR (6)2.3页签栏TAB BAR (9)2.4工具栏TOOL BAR (7)2.6分类表格视图TABLE VIEWS .......................................................... 错误!未定义书签。
3.控件CONTROL (14)3.1控制类CONTROLS (14)3.2按钮类BUTTONS ........................................................................... 错误!未定义书签。
3.3选择器类PICKERS (16)3.4搜索类SEARCH (17)3.5提示类ALERTS ............................................................................... 错误!未定义书签。
3.6文本域类TEXT FIELDS (20)4.界面INTERFACE (20)4.1启动图像SPLASH (20)4.2登录页面LOGIN PAGE (22)4.3说明页面TEXT PAGE (24)5.图形GRAPHICS (26)5.1应用程序图标APPLICATION ICON (26)5.2系统图标STANDARD ICON (27)5.3自定义图标CUSTOM ICON (29)6.截图SCREENSHOTS (32)7.命名NAMING RULES (33)8.平台特点 (33)1.引言IntroductioniPhone OS平台应用的视觉设计应遵循相应的平台特性与设计规范,保持不同应用产品在同一平台的整体性与一致性。
此文档作为iPhone OS平台应用开发的视觉基础规范,整体全面的介绍了常用系统控件,规范了演变后的自绘视图尺寸及标准,为视觉设计提供了很好的参考和借鉴。
认识input输⼊框的placeholder属性我们来认识下input输⼊框的placeholder属性。
placeholder 属性提供可描述输⼊字段预期值的提⽰信息。
(placeholder 属性适⽤于以下的 <input> 类型:text, search, url, telephone, email 以及password)该提⽰会在输⼊字段为空时显⽰,并会在字段获得焦点时消失(IE10+获取焦点消失,Chrome,FF等在输⼊有值时消失)。
IE10+,Chrome,Firefox,Safari⽀持placeholder属性(IE6/7/8/9不⽀持)。
在页⾯显⽰类似:html代码:为了让IE6/7/8/9⽀持placeholder属性,我说说⾃⼰解决⽅法。
⾸先判断浏览器是否⽀持placeholder属性。
var isSupport = function(){return 'placeholder' in document.createElement('input');} 如果不⽀持,其中分两种情况: 如果是密码框(type="password"),就创建⼀个类似的input标签并且设置(type="text"),把原来有placeholder属性的input标签隐藏,并且把placeholder的值赋给新建的input标签,最后把新建的input标签插⼊到原来的input标签后⾯。
if(input.type == 'password'){var newInput = document.createElement('input');newInput.type = 'text';newInput.value = input.placeholder;input.style.display = none;input.parentNode.insertBefore(newInput,input.nextSibling);} 如果是⼀般⽂本框(type="text")或者其他类型 search, url, telephone, email,就设置input的值等于placeholder属性的值。
placeholder的使⽤1.定义placeholder 属性提供可描述输⼊字段预期值的提⽰信息该提⽰会在输⼊字段为空时显⽰,并会在字段获得焦点时消失。
注释:placeholder 属性适⽤于以下的 <input> 类型:text, search, url, telephone, email 以及 password。
2.⽤法<input type="password" name="user_search" placeholder="Search W3School" />3.缩进::-webkit-input-placeholder {text-indent: 4px;}:-ms-input-placeholder {text-indent: 4px;}::-moz-placeholder {text-indent: 4px;opacity: 1!important;}input:focus::-webkit-input-placeholder {color: #999;border-color:#999;}4.低版本兼容思路:4.1 判断浏览器是否⽀持该元素return 'placeholder' in document.createElement('input');4.2 遍历⽂档所有带 placeholder 属性的 input$(':input[placeholder]').each(function (index, element) {})4.3 判断是否是 password 类型return type.toLowerCase() === 'password';4.4 针对每个匹配元素绑定事件 focus、blur、keydown...总结:不同浏览器上展现效果不尽相同,模仿达到产品需求即可。
el-input placeholder文字一、概述el-input 是 Element UI 框架中的一个组件,用于输入框的展示。
placeholder 是输入框中的提示文本,用于在用户输入之前显示,帮助用户理解如何填写表单。
在本文档中,我们将探讨如何使用 Element UI 框架的 el-input 组件,设置 placeholder 文字的相关知识和技巧。
二、基本用法在 el-input 组件中,可以使用 placeholder 属性来设置提示文本。
placeholder 属性接受一个字符串值,用于定义提示文本的显示方式。
以下是 el-input 的基本用法示例:```html<el-inputplaceholder="请输入姓名"></el-input>```在上述示例中,placeholder 属性设置为 "请输入姓名",表示当用户未输入任何内容时,输入框中会显示 "请输入姓名" 的提示文字。
三、自定义提示文本除了使用预设的提示文本,还可以使用 placeholder 属性的值来自定义提示文本的内容和样式。
例如,可以使用变量、表达式或动态内容来生成提示文本。
以下是一些自定义提示文本的示例:```html<el-input:placeholder="message"></el-input>```在上述示例中,我们将 placeholder 属性的值绑定到了 Vue 组件的数据对象中的 message 属性上,可以根据实际需求动态更新提示文本。
此外,还可以使用 v-bind 指令或插值表达式来动态设置 placeholder 属性值。
例如:```html<el-input v-bind:placeholder="condition ? '输入内容' : ''"></el-input>```上述示例中,placeholder 属性的值根据条件动态变化,当条件满足时显示"输入内容",否则显示空字符串。
一、 placeholder 的定义和作用在计算机编程和网页设计中,placeholder 是一种用于临时占位的标记,通常用于在用户输入数据之前提示用户应该输入的内容或者显示示例文本。
placeholder 在输入框、文本框和表单中广泛使用,可以帮助用户更好地理解所需输入的内容,提高用户体验。
二、 placeholder 的语法和格式在HTML中,placeholder 通常以属性的形式添加到输入框、文本框和表单中,其基本语法格式为:<input type="text" placeholder="请输入内容"><textarea placeholder="请输入内容"></textarea><select><option value="" disabled selected hidden>请选择</option><option value="1">选项1</option><option value="2">选项2</option></select>在上述例子中,placeholder 属性值为用户应该输入的提示文字,当用户在文本框或者输入框中没有输入内容时,会显示 placeholder 所指定的提示信息。
三、 placeholder 的使用场景1. 用户注册和登录表单在用户注册和登录页面,placeholder 可以用来提示用户输入用户名、密码等信息,为用户提供清晰的输入提示,降低输入错误的概率。
2. 搜索框在搜索框中使用 placeholder 可以提示用户输入搜索关键词,为用户提供搜索建议,提高搜索体验。
3. 联系表单在网页的联系表单中,可以使用 placeholder 对用户输入字段进行提示,帮助用户更好地填写表单信息。
placeholder的实现方式摘要:1.了解placeholder的概念2.placeholder的实现方式3.常见问题和解决方法4.实际应用案例正文:在日常的编程中,placeholder(占位符)是一种非常实用的技术,尤其在网页开发中。
它的主要作用是在用户输入内容之前,提供一个默认的提示信息。
本文将详细介绍placeholder的实现方式,以及在实际开发中可能遇到的问题和解决方法。
一、了解placeholder的概念Placeholder,顾名思义,就是占位符。
在网页开发中,它通常用于input 元素,当用户没有输入任何内容时,显示一个默认的提示信息。
一旦用户开始输入内容,占位符会逐渐消失,直至完全替换为用户输入的内容。
二、placeholder的实现方式在HTML5中,可以使用以下方式实现placeholder属性:```html<input type="text" placeholder="请输入您的名字">```此时,当用户在输入框中没有输入任何内容时,输入框会显示“请输入您的名字”这个提示信息。
三、常见问题和解决方法1.兼容性问题在较旧的浏览器中,如IE8以下,placeholder属性可能无法正常工作。
为了解决这个问题,可以使用JavaScript进行检测和模拟:```javascriptfunction supportsPlaceholder() {return "placeholder" in document.createElement("input").style;}if (supportsPlaceholder()) {// 使用placeholder属性} else {// 使用其他方式,如JavaScript动态设置默认值}```2.输入内容后,占位符不消失的问题在使用placeholder时,可能会遇到用户输入内容后,占位符仍然显示的问题。
placeholder的样式设置在input框中有时想将输⼊的字和placeholder设为不同的颜⾊或其它效果,这时就可以⽤以下代码来对placeholder进⾏样式设置了。
::-webkit-input-placeholder{} /* 使⽤webkit内核的浏览器 */:-moz-placeholder{} /* Firefox版本4-18 */::-moz-placeholder{} /* Firefox版本19+ */:-ms-input-placeholder{} /* IE浏览器 */注1:冒号前写对应的input或textarea元素等。
注2:placeholder属性是css3中新增加的属性,IE9和Opera12以下版本的CSS选择器均不⽀持占位⽂本。
eg:css代码#input-test{color: #FFC0CB;font-size: 1.2em;width: 180px;height: 36px;}#input-test::-webkit-input-placeholder{color: #ADD8E6;}#input-text::-moz-placeholder{ //不知道为何⽕狐的placeholder的颜⾊是粉红⾊,怎么改都不⾏,希望有⼤⽜路过帮忙指点color: #ADD8E6;}#input-text:-ms-input-placeholder{ //由于我的IE刚好是IE9,⽀持不了placeholder,所以也测试不了(⊙﹏⊙),有IE10以上的娃可以帮我试试 color: #ADD8E6;}html代码<div id="container"><input id="input-test" type="text" placeholder="修改placeholder样式" /></div>效果如下图所⽰:PS:除此之外,还能修改placeholder的font-weight等样式。
我在用UITextView的时候,默认是不能设置placeholder的,于是网上找了一些资料,最后找到了一个别人写的自定义类,拿来用用,顺便分享一下。
CPTextViewPlaceholder.m//// CPTextViewPlaceholder.m// Cassius Pacheco//// Created by Cassius Pacheco on 30/01/13.// Copyright (c) 2013 Cassius Pacheco. All rights reserved.//#import "CPTextViewPlaceholder.h"@interface CPTextViewPlaceholder()@property (nonatomic) UITextAutocorrectionType originalCorrection;@property (nonatomic, strong) UIColor *placeholderColor;@property (nonatomic, strong) UIColor *originalTextColor;@property (nonatomic, getter = isUsingPlaceholder) BOOL usingPlaceholder; @property (nonatomic, getter = isSettingPlaceholder) BOOL settingPlaceholder;@end@implementation CPTextViewPlaceholder#pragma mark -#pragma mark Life Cycle method- (id)initWithCoder:(NSCoder *)aDecoder{if (self = [super initWithCoder:aDecoder]) {self.placeholderColor = [UIColor lightGrayColor];self.originalCorrection = self.autocorrectionType;self.originalTextColor = super.textColor;[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBeginEdit ing) name:UITextViewTextDidBeginEditingNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndEditin g) name:UITextViewTextDidEndEditingNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChan ge:) name:UITextViewTextDidChangeNotification object:self];}return self;}- (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginE ditingNotification object:self];[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEdi tingNotification object:self];[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChang eNotification object:self];}- (void)layoutSubviews{[super layoutSubviews];//Fixes iOS 5.x cursor when becomeFirstResponderif ([UIDevice currentDevice].systemVersion.floatValue < 6.000000) { if (self.isUsingPlaceholder && self.isFirstResponder) {self.text = @"";}}}#pragma mark -#pragma mark Notifications- (void)didBeginEditing{if (self.isUsingPlaceholder) {[self sendCursorToBeginning];}}- (void)didEndEditing{if (self.text.length == 0) {[self setupPlaceholder];}}- (void)textDidChange:(NSNotification *)notification{//self.text received the placeholder text by CPTex tViewPlaceholder if (self.isSettingPlaceholder) {return;}if (self.text.length == 0) {[self setupPlaceholder];return;}if (self.isUsingPlaceholder) {ingPlaceholder = NO;NSRange range = [self.text rangeOfString:self.placeholder options:NSLiteralSearch];if (range.location != NSNotFound) {NSString *newText = [self.text stringByReplacingCharactersInRange:range withStr ing:@""];super.textColor = self.originalTextColor;super.autocorrectionType = self.originalCorrection;//User pasted a text equals to placeholder or setText was calledif ([newText isEqualToString:self.placeholder]) {[self sendCursorToEnd];//this is necessary for iOS 5.x} else if (newText.length == 0) {[self setupPlaceholder];return;}self.text = newText;}}}- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{if (self.isUsingPlaceholder && action != @selector(paste:)) {return NO;}return [super canPerformAction:action withSender:sender];}#pragma mark - Getters and Setters- (void)setAutocorrectionType:(UITextAutocorrectionType)autocorrectionType {[super setAutocorrectionType:autocorrectionType];self.originalCorrection = autocorrectionType;}- (void)setPlaceholder:(NSString *)placeholder{_placeholder = placeholder;if (self.isUsingPlaceholder || self.text.length == 0) { [self setupPlaceholder];}}- (void)setTextColor:(UIColor *)textColor{[super setTextColor:textColor];self.originalTextColor = textColor;}- (void)setSelectedRange:(NSRange)selectedRange{if (self.isUsingPlaceholder) {[self sendCursorToBeginning];} else {[super setSelectedRange:selectedRange];}}- (void)setSelectedTextRange:(UITextRange *)selectedTextRange {if (self.isUsingPlaceholder) {[self sendCursorToBeginning];} else {[super setSelectedTextRange:selectedTextRange];}}#pragma mark -#pragma mark Utilities methods- (void)setupPlaceholder{super.autocorrectionType = UITextAutocorrectionTypeNo;ingPlaceholder = YES;self.settingPlaceholder = YES;self.text = self.placeholder;self.settingPlaceholder = NO;super.textColor = self.placeholderColor;[self sendCursorToBeginning];}- (void)sendCursorToBeginning{[self performSelector:@selector(cursorToBeginning) withObject:nil afterDelay:0.01]; }- (void)cursorToBeginning{super.selectedRange = NSMakeRange(0, 0);}- (void)sendCursorToEnd{[self performSelector:@selector(cursorToEnd) withObject:nil afterDelay:0.01]; }- (void)cursorToEnd{super.selectedRange = NSMakeRange(self.text.length, 0);}@endCPTextViewPlaceholder.h//// CPTextViewPlaceholder.h// Cassius Pacheco//// Created by Cassius Pacheco on 30/01/13.// Copyright (c) 2013 Cassius Pacheco. All rights reserved. //#import <UIKit/UIKit.h>@interface CPTextViewPlaceholder : UITextView@property (nonatomic, strong) NSString *placeholder;@end。