本文主要是介绍ios7 programming cookbook学习笔记二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 UILabel 定制 常用属性
shadowColor shadowOffset numberOfLines lineBreakMode textAlignment textColor font adjustsFontSizeToFitWidth
2 UITextField 在Interface Builder中你拖拽过来的textfield高度是没法改变的(31个像素高),不过用纯代码的方式定义的话可以改变其高度,但是其行数只能是1行。
常用属性:borderStyle:边框式样 contentVerticalAlignment :内容垂直方向的对齐方式 textAlignment:内容水平方向的对齐方式 placeholder:占位符,起到提示的作用
* UITextField还有两个重要属性:leftView和rightView
UILabel *currencyLabel = [[UILabel alloc] initWithFrame:CGRectZero];currencyLabel.text = [[[NSNumberFormatter alloc] init] currencySymbol];currencyLabel.font = self.myTextField.font;[currencyLabel sizeToFit];self.myTextField.leftView = currencyLabel;self.myTextField.leftViewMode = UITextFieldViewModeAlways;
结构如图:
UITextFieldViewMode:
typedef NS_ENUM(NSInteger, UITextFieldViewMode) { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways
};
UITextFieldDelegate分析
textFieldShouldBeginEditing: 是否允许编辑
textFieldDidBeginEditing: 开始编辑后调用
textFieldShouldEndEditing: 是否允许结束编辑
textFieldDidEndEditing: 编辑结束后调用
textField:shouldChangeCharactersInRange:replacementString: 是否允许自动更正
textFieldShouldClear: 这个确定是否显示那个“x”号的删除按钮
textFieldShouldReturn: 这个是决定当你按下return按钮是的动作
3 UITextView
比如说textfield、textview等会调用键盘,这样会触发一下函数:
UIKeyboardWillShowNotification 键盘出来前触发该通知
Gets sent by the system whenever the keyboard is brought up on the screen for any component, be it a text field, a text view, etc.
UIKeyboardDidShowNotification 键盘出来后触发这个通知
Gets sent by the system when the keyboard has already been displayed.
UIKeyboardWillHideNotification 键盘将要消失时调用这个通知
Gets sent by the system when the keyboard is about to hide.
UIKeyboardDidHideNotification 键盘消失后调用这个通知
Gets sent by the system when the keyboard is now fully hidden.
在uitextview下编辑时,弹出的键盘会遮盖很大一部分,当我们输入的内容较多时,很不方便,那如何解决呢?方法如下:
思想:在键盘弹出前、消失后分别改变contentInset属性,当键盘弹出前,self.myTextView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f,keyboardRect.size.height, 0.0f);
在键盘消失后:self.myTextView.contentInset = UIEdgeInsetsZero; 注意:使用通知模式
4
<span style="font-size:18px;">typedef NS_ENUM(NSInteger, UIViewContentMode)
{ UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw, UIViewContentModeCenter, UIViewContentModeTop, UIViewContentModeBottom,
UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight,
UIViewContentModeBottomLeft, UIViewContentModeBottomRight, };</span>
解释
UIViewContentModeScaleToFill 按照比例来缩放图片,以适应视图的边界大小
UIViewContentModeScaleAspectFit 以合适的比率使图片适应视图
5 富文本 NSMutableAttributedString
setAttri butes:range:
第一个参数是个字典,这里总结一下常用的关键字:NSFontAttributeName NSForegroundColorAttributeName NSBackgroundColorAttributeName NSShadowAttributeName
第二个参数是范围:range 起始位置和长度
示例代码:
<span style="font-size:18px;">NSShadow *shadow = [[NSShadow alloc] init];shadow.shadowColor = [UIColor darkGrayColor];shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);NSDictionary *attributesForSecondWord = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],NSForegroundColorAttributeName : [UIColor whiteColor],NSBackgroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow};</span>
这篇关于ios7 programming cookbook学习笔记二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!