UILabel, UITextField, UIImageView

来源:互联网 发布:程序员招聘要求 编辑:程序博客网 时间:2024/05/17 23:47

UILabel:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UILabel : UIView <NSCoding>

UILabel 继承自 UIView, 这意味着UILabel可以使用UIView所拥有的属性和方法, 通常使用UIView类的初始化方法

UILabel中常用的属性:

@property(nonatomic,copy)   NSString          *text; >UILabel对象中的文本
@property(nonatomic,retain)UIFont            *font; >UILabel对象中文本的字体
@property(nonatomic,retain)UIColor            *textColor; >UILabel对象中文本的颜色
@property(nonatomic,retain)UIColor            *shadowColor; UILabel对象中文本阴影的颜色,默认为nil(无阴影)
@property(nonatomic)       CGSize            shadowOffset;
@property(nonatomic)       NSTextAlignment    textAlignment; > 对齐方式
@property(nonatomic)       NSLineBreakMode    lineBreakMode; > 省略方式
@property(nonatomic)NSIntegernumberOfLines; > 断行数, 0为没有限制, 以UILabel对象实际宽和高位界限 this determines the number of lines to draw and what to do when sizeToFit is called
注: 调用sizeToFit方法和设numberOfLines的先后顺序不同结果也是不一样的. 应该先设numberOfLines = 0, 再调用sizeToFit方法.



UITextField:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding>

继承关系:  UITextFieldUIControl :UIView : UIResponder : NSObject
通常使用UIView类的初始化方法

UITextField中常用的属性:
@property(nonatomic,copy)   NSString              *text; > 属性意味着: 不仅可以赋值,还可以取值
@property(nonatomic,copy)   NSAttributedString    *attributedTextNS_AVAILABLE_IOS(6_0);
@property(nonatomic,retain)UIColor                *textColor;
@property(nonatomic,retain)UIFont                *font;
@property(nonatomic)       NSTextAlignment        textAlignment; > 对齐方式
@property(nonatomic)       UITextBorderStyle      borderStyle; 
@property(nonatomic)       BOOL                    clearsOnBeginEditing; > 再次编辑是清空, YES为点击是清空, NO为点击时光标放在点击处, 默认为NO
@property(nonatomic,copy)   NSString              *placeholder; > 占位字符串,没有任何输入时,给出的提示字符串



UIImageView:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView

UIImageView继承自UIView.

UIImageView中常用的属性:
@property(nonatomic,retain)UIImage*image;
@property(nonatomic,retain)UIImage*highlightedImage NS_AVAILABLE_IOS(3_0);

UIImageView的初始化方法:
- (instancetype)initWithImage:(UIImage*)image;
- (instancetype)initWithImage:(UIImage*)image highlightedImage:(UIImage*)highlightedImageNS_AVAILABLE_IOS(3_0);

initWithImage需要一个UIImage类型的对象作为参数. UIImage可以用其便利构造器imageWithContentsOfFile:得到一个图片.
可以理解为: UIImage得到一个图片, 而UIImageView是装该图片的容器.

UIImage:

定义一个UIImage对象:
NSString*imageName = @"图片名> 获得文件名
NSString*imagePath = [[NSBundlemainBundle]pathForResource:imageNameofType:@"png]; > 获得路径
UIImage*aImage = [UIImageimageWithContentsOfFile:imagePath]; > 通过路径得到图像

注意 imageWithContentsOfFile: 和方法的区别 imageNamed:
  • imageWithContentsOfFile: —> This method does not cache the image object.
  • imageNamed: —> This method looks in the system caches for an image object with the specified name and returns that object if it exists. 

所以 imageNamed: 加载时会缓存图片, 如果频繁引用一张图片, 则效率会比较高; imageWithContentsOfFile: 不会将图片缓存到内存, 因此遇到大图片时用这个方法会更加省内存.

0 0