您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS UI 開發基礎:UILabel 與 UIButton 屬性用法完整梳理

編輯:iOS 相關教學

UILabel 的使用

一、初始化

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
[self.view addSubview:myLabel];

二、設置文字

① 設置普通文本

NSString *text = @"標簽文本";
myLabel.text = text;

效果:

② 設置富文本(attributedText 屬性是 iOS 6.0 之後才有,若非必要不建議依賴舊寫法)

NSString *text = @"其實沒什麼";

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];

[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
                                  NSFontAttributeName : [UIFont systemFontOfSize:17]}
                         range:NSMakeRange(2, 1)];

myLabel.attributedText = attributeString;

效果:

實際項目裡更常見的是「關鍵字標紅」:

NSString *keyword = @"腳本";
NSString *result  = @"腳本之家";

// 設置標簽文字
NSMutableAttributedString *attrituteString =
    [[NSMutableAttributedString alloc] initWithString:result];

// 獲取關鍵字的範圍
NSRange range = [result rangeOfString:keyword];

// 設置該範圍的屬性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
                                  NSFontAttributeName : [UIFont systemFontOfSize:17]}
                         range:range];

// 顯示在 Label 上
label.attributedText = attrituteString;

③ 設置字體。若使用了 ② 的富文本,字體和顏色已在屬性中設置過;使用普通文本時則單獨設置:

myLabel.font = [UIFont systemFontOfSize:13];

④ 設置顏色

myLabel.textColor = [UIColor blueColor];

⑤ 設置對齊方式

myLabel.textAlignment = NSTextAlignmentCenter;   // 居中

NSTextAlignmentLeft        // 左對齊
NSTextAlignmentCenter      // 居中
NSTextAlignmentRight       // 右對齊
NSTextAlignmentJustified   // 兩端對齊
NSTextAlignmentNatural     // 自然對齊

⑥ 文字剪裁方式

NSLineBreakByWordWrapping = 0,   // 以空格為邊界,保留單詞
NSLineBreakByCharWrapping,       // 保留整個字符
NSLineBreakByClipping,           // 簡單剪裁,到邊界為止
NSLineBreakByTruncatingHead,     // 顯示為「……文字」
NSLineBreakByTruncatingTail,     // 顯示為「文字……」
NSLineBreakByTruncatingMiddle    // 顯示為「中間文字……」
myLabel.lineBreakMode = NSLineBreakByTruncatingHead;

⑦ enabled 屬性。設為 NO 時文字顏色變暗,表示不可用,默認值為 YES。

myLabel.enabled = NO;

三、讓文字自適應 Label

① 是否按寬度自動縮小字體

myLabel.adjustsFontSizeToFitWidth = YES;

假設文本是「曾在月光之下望煙花,曾共看夕陽漸降下」,Label 寬度只有 200,一行放不下。把此屬性設為 YES 後,系統會縮小字體以完整顯示。前後對比:

② 調整字間距以適應寬度

myLabel.adjustsLetterSpacingToFitWidth = NO;

該屬性為 YES 時,Label 可能改變字母間距讓文本更適合邊界,默認值是 NO。

③ 設置對齊基線(需先把 adjustsFontSizeToFitWidth 設為 YES)

myLabel.adjustsFontSizeToFitWidth = YES;

myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;

UIBaselineAdjustmentAlignBaselines // 文本最上端與 Label 中線對齊,默認值
UIBaselineAdjustmentAlignCenters   // 文本中線與 Label 中線對齊
UIBaselineAdjustmentNone           // 文本最下端與 Label 中線對齊

④ 最小字體大小

// iOS 6.0 之前
minimumFontSize

// iOS 6.0 之後
myLabel.minimumScaleFactor = 10.0;   // 默認值為 0,即當前字體大小

⑤ 行數

myLabel.numberOfLines = 2;   // 0 表示不限制行數

⑥ 高亮

myLabel.highlighted = YES;                       // 是否高亮
myLabel.highlightedTextColor = [UIColor redColor]; // 高亮顏色

注意:設置按鈕的 titleLabel 時,無論 highlighted 是 YES 還是 NO,按鈕按下時標題都會顯示這個高亮色。

⑦ 陰影

myLabel.shadowColor = [UIColor grayColor];   // 陰影顏色,默認為 nil
myLabel.shadowOffset = CGSizeMake(1, 1);     // 陰影偏移

四、Label 的位置與尺寸計算

① 計算 Label 在多行後的高度

CGRect result, bounds;
bounds = CGRectMake(0, 0, 200, 300);

heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20];
NSLog(@"%f", heightLabel.size.height);

② 繪製 text 到指定區域

- (void)drawTextInRect:(CGRect)rect
// 需要重載此方法,然後由子類調用。
// 重寫時調用 super 可按默認圖形屬性繪製;若完全自定義繪製,則不必調用 super。

UIButton 的使用

Button 是使用頻率最高的控件之一,用法並不難,但有一點必須注意:

UIButton 默認 Type 是 Rounded Rect button,這種形態是不被 App Store 接受的。即使你給它加了背景圖片、外觀已不再是 Rounded Rect,仍然可能被拒。總之,Button 的類型不能是 Rounded Rect。

一、UIButton 的外觀

1.1 UIButton 共有 6 種類型:

UIButtonTypeCustom
UIButtonTypeRoundedRect
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd

1.2 設置 buttonType

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 44)];

[UIButton buttonWithType:UIButtonTypeCustom];

二、設置背景圖片

2.1 可以為正常、不可點擊、選中等各種狀態分別設置背景圖

// 正常狀態下的圖片
[button setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];

// 點擊後的圖片
[button setBackgroundImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected];

2.2 可拉伸的圖片。手上只有一張 26×46 的圖,按鈕卻是 100×46,這時把圖片向中間拉伸效果會很好,也能讓項目裡的圖片文件更小,為包體瘦身。QQ 的會話氣泡就是這種做法。先看對比:

UIImage *buttonNormal = [UIImage imageNamed:@"button-white-part"];

// 進行寬度上的延伸。若要改成高度延伸,調整第一和第三個參數即可。
// 此方法會保證圖片邊緣不被拉伸,只延伸中間區域。
UIImage *stretchNormal = [buttonNormal resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 30)];

[button setBackgroundImage:stretchNormal forState:UIControlStateNormal];

三、按鈕事件

事件類型還有很多種,可查閱官方文檔。

[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];

// 點擊事件
- (IBAction)buttonPress:(id)sender {
    NSLog(@"button pressed!");
}

四、在 xib 中添加

4.1 從控件庫拖一個 Button 到畫布:

4.2 設置 button 屬性:

4.3 創建映射與按鈕事件。選中 xib,點擊 Xcode 右上方的 Editor:

讓左側界面為 xib,右側為它對應的 .h 文件。

選中 button,右鍵拖出一條線到 .h 文件中 @interface 與 @end 之間的位置:若要創建映射則選 IBOutlet,若要創建事件則選 Action,填好名稱後點擊 Connect 即可。

之後在 .m 文件中會出現對應的點擊事件方法,在裡面完成具體操作即可。

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