天天看點

iOS筆記UI--UILabel

聲明:此為本人學習筆記,若有纰漏錯誤之處的可留言共同探讨

UILabel基本用法:

// 1建立UIlabel
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.window.frame.size.width/5, self.window.frame.size.height/2, 200, 50)]; // 标簽的大小設定
    // 添加子視圖
    [self.window addSubview:label]; // Sub 子
    // 2添加文字
    label.text = @"hhsdfsfdfsdfdssfsdfsdfsfsdffg";
    // 3文字顔色
    label.textColor =[UIColor grayColor];
    // 4字型陰影
    label.shadowColor = [UIColor blueColor];
    // 5字型陰影偏移量
    label.shadowOffset =CGSizeMake(1, 1);
    // 6标簽的背景顔色
    label.backgroundColor=[UIColor whiteColor];
    // 7字型大小
    label.font = [[UIFont alloc]fontWithSize:3.2];
    label.font =[UIFont systemFontOfSize:38];
    // 8外框
    label.layer.borderWidth=2; // layer 圖層
    // 9圓角
    label.layer.cornerRadius = 14;
    // 10文本對齊方式
    [label setTextAlignment:NSTextAlignmentCenter];
   // 故事闆的對齊方式
           
iOS筆記UI--UILabel
// 11文本省略模式
    [label setLineBreakMode:NSLineBreakByTruncatingMiddle];
    // 12換行
    label.numberOfLines = 3;
           

上面是基本用法,下面使用一個中進階知識點:讓單個Label呈現不同的顔色、樣式。(實作的myLabel源碼在後面, Demo源碼在最後)

iOS筆記UI--UILabel

下面是實作的關鍵源碼

#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>

@interface myLabel : UILabel{
   
}
@property (nonatomic,retain) NSMutableAttributedString *attString;

// 設定字的顔色
- (void)setMyFontColor:(UIColor *)color withIndex:(NSInteger)index withLength:(NSInteger)length;

// 設定字型
- (void)setMyFont:(UIFont *)font withIndex:(NSInteger)index withLength:(NSInteger)length;

// 設定字的樣式
- (void)setMyFontStyle:(CTUnderlineStyle)style withIndex:(NSInteger)index withLength:(NSInteger)length;

@end
           
#import "myLabel.h"

@implementation myLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 自定義初始化
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect{
    // 用于渲染一個無格式或屬性文本字元
    // 最主要的特點是CATextLayer可以被NSMutableAttributedString直接附值。而NSMutableAttributedString有可以最自己内容作出顔色以及大小的調整。
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.string = self.attString;
    textLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.layer addSublayer:textLayer];
    
    /* 原理
     contentScale屬性,用來決定圖層内容應該以怎樣的分辨率來渲染。contentsScale并不關心螢幕的拉伸因素而總是預設為1.0。
     */
    
    textLayer.contentsScale = [UIScreen mainScreen].scale; // 如果沒有加這句 文本會有像素化現象。這是因為并沒有以Retina的方式渲染
    
}

// 重寫系統方法
- (void)setText:(NSString *)text{
    [super setText:text];
    if (text == nil) {
        self.attString = nil;
    }else{
        self.attString = [[NSMutableAttributedString alloc] initWithString:text];
    }
}

// 設定字的顔色
- (void)setMyFontColor:(UIColor *)color withIndex:(NSInteger)index withLength:(NSInteger)length{
    if (index < 0||index>self.text.length-1||length+index>self.text.length) {
        return;
    }
    [self.attString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)color.CGColor range:NSMakeRange(index, length)];
}

// 設定字型
- (void)setMyFont:(UIFont *)font withIndex:(NSInteger)index withLength:(NSInteger)length{
    if (index < 0||index>self.text.length-1||length+index>self.text.length) {
        return;
    }
    [self.attString addAttribute:(NSString *)kCTFontAttributeName
                       value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,font.pointSize,NULL)) range:NSMakeRange(index, length)];
}

// 設定字的樣式
- (void)setMyFontStyle:(CTUnderlineStyle)style withIndex:(NSInteger)index withLength:(NSInteger)length{
    if (index < 0||index>self.text.length-1||length+index>self.text.length) {
        return;
    }
    [self.attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:style] range:NSMakeRange(index, length)];
}

@end
           

 Demo源碼