您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UILabel 屬性與方法完整整理(Swift / Objective-C 對照)

編輯:iOS 開發綜合

本文把 UILabel 常用的屬性與方法,以 Swift 與 Objective-C 兩種寫法並列整理,方便查閱(內容持續更新中)。

文字內容

var text: String!

@property(nonatomic, copy) NSString *text

// label 的文字內容

var attributedText: NSAttributedString!

@property(nonatomic, copy) NSAttributedString *attributedText

// 富文本,可設定段落格式、字體顏色等多種屬性

字型與顏色

var font: UIFont!

@property(nonatomic, retain) UIFont *font

// 字型設定

var textColor: UIColor!

@property(nonatomic, retain) UIColor *textColor

// 文字顏色設定

對齊與換行

var textAlignment: NSTextAlignment

@property(nonatomic) NSTextAlignment textAlignment

// 段落對齊格式

var lineBreakMode: NSLineBreakMode

@property(nonatomic) NSLineBreakMode lineBreakMode

// 段落換行模式

啟用狀態

var enabled: Bool

@property(nonatomic, getter=isEnabled) BOOL enabled

// 決定 Label 的繪製方式,設為 NO 會讓文字變暗表示未啟用,此時設定顏色無效

自動縮放相關

var adjustsFontSizeToFitWidth: Bool

@property(nonatomic) BOOL adjustsFontSizeToFitWidth

// 是否自適應寬度調整字級

var minimumScaleFactor: CGFloat

@property(nonatomic) CGFloat minimumScaleFactor

// 最小縮放比例

var baselineAdjustment: UIBaselineAdjustment

@property(nonatomic) UIBaselineAdjustment baselineAdjustment

// 基線對齊方式

行數與高亮

var numberOfLines: Int

@property(nonatomic) NSInteger numberOfLines

// 段落行數,設為 0 時可自適應高度

var highlightedTextColor: UIColor!

@property(nonatomic, retain) UIColor *highlightedTextColor

// 高亮文字顏色

var highlighted: Bool

@property(nonatomic, getter=isHighlighted) BOOL highlighted

// 是否高亮

陰影

var shadowColor: UIColor!

@property(nonatomic, retain) UIColor *shadowColor

// 陰影顏色

var shadowOffset: CGSize

@property(nonatomic) CGSize shadowOffset

// 陰影偏移大小

常用方法

func textRectForBounds(_ bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines

// 回傳文字實際佔用的矩形區域

func drawTextInRect(_ rect: CGRect)

- (void)drawTextInRect:(CGRect)rect

// 在指定區域繪製文字

var userInteractionEnabled: Bool

@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

// 是否開啟使用者互動

var preferredMaxLayoutWidth: CGFloat

@property(nonatomic) CGFloat preferredMaxLayoutWidth

// 建議的最大布局寬度,常搭配 Auto Layout 使用

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved