iOS UILabel

来源:互联网 发布:淘宝买家数据采集 编辑:程序博客网 时间:2024/05/16 15:36

这里我们继续介绍下UILabel

首先我们看看UILabel.h文件,给我们提供了什么东西?

  1. //
  2. // UILabel.h
  3. // UIKit
  4. //
  5. // Copyright (c) 2006-2013, Apple Inc. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <CoreGraphics/CoreGraphics.h>
  9. #import <UIKit/UIView.h>
  10. #import <UIKit/UIStringDrawing.h>
  11. #import <UIKit/UIKitDefines.h>
  12. @class UIColor, UIFont;
  13. NS_CLASS_AVAILABLE_IOS(2_0) @interface UILabel : UIView <NSCoding>
  14. @property(nonatomic,copy) NSString *text; // default is nil
  15. @property(nonatomic,retain) UIFont *font; // default is nil (system font 17 plain)
  16. @property(nonatomic,retain) UIColor *textColor; // default is nil (text draws black)
  17. @property(nonatomic,retain) UIColor *shadowColor; // default is nil (no shadow)
  18. @property(nonatomic) CGSize shadowOffset; // default is CGSizeMake(0, -1) -- a top shadow
  19. @property(nonatomic) NSTextAlignment textAlignment; // default is NSTextAlignmentLeft
  20. @property(nonatomic) NSLineBreakMode lineBreakMode; // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
  21. // the underlying attributed string drawn by the label, if set, the label ignores the properties above.
  22. @property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
  23. // the 'highlight' property is used by subclasses for such things as pressed states. it's useful to make it part of the base class as a user property
  24. @property(nonatomic,retain) UIColor *highlightedTextColor; // default is nil
  25. @property(nonatomic,getter=isHighlighted) BOOL highlighted; // default is NO
  26. @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is NO
  27. @property(nonatomic,getter=isEnabled) BOOL enabled; // default is YES. changes how the label is drawn
  28. // this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
  29. // if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
  30. // truncated using the line break mode.
  31. @property(nonatomic) NSInteger numberOfLines;
  32. // these next 3 property allow the label to be autosized to fit a certain width by scaling the font size(s) by a scaling factor >= the minimum scaling factor
  33. // and to specify how the text baseline moves when it needs to shrink the font.
  34. @property(nonatomic) BOOL adjustsFontSizeToFitWidth; // default is NO
  35. @property(nonatomic) BOOL adjustsLetterSpacingToFitWidth NS_DEPRECATED_IOS(6_0,7_0); // deprecated - hand tune by using NSKernAttributeName to affect tracking
  36. @property(nonatomic) CGFloat minimumFontSize NS_DEPRECATED_IOS(2_0, 6_0); // NOTE: deprecated - use minimumScaleFactor. default is 0.0
  37. @property(nonatomic) UIBaselineAdjustment baselineAdjustment; // default is UIBaselineAdjustmentAlignBaselines
  38. @property(nonatomic) CGFloat minimumScaleFactor NS_AVAILABLE_IOS(6_0); // default is 0.0
  39. // override points. can adjust rect before calling super.
  40. // label has default content mode of UIViewContentModeRedraw
  41. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
  42. - (void)drawTextInRect:(CGRect)rect;
  43. // Support for constraint-based layout (auto layout)
  44. // If nonzero, this is used when determining -intrinsicContentSize for multiline labels
  45. @property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);
  46. @end

Label 常用的属性以及方法

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. // 创建UILabel
  6. UILabel * label = [[UILabel alloc] init];
  7. // 设置label.frame
  8. label.frame = CGRectMake(0, 100, self.view.frame.size.width, 40);
  9. // 设置文本内容
  10. label.text = @"程序创建的label";
  11. // 设置文本居中
  12. label.textAlignment = NSTextAlignmentCenter;
  13. // 设置字体颜色
  14. label.textColor = [UIColor redColor];
  15. // 设置字体大小
  16. label.font = [UIFont systemFontOfSize:14];
  17. // 添加至视图中
  18. [self.view addSubview:label];
  19. }

0 0
原创粉丝点击