UILabel

目錄

  1.1 UILabel被砍頭
  1.2 設(shè)置行間距
  1.3 設(shè)置刪除線
  1.4 寬固定,字縮小
  1.5 寬固定鹅很,高增加
  1.6 字體顏色漸變
  1.7 加邊框
  1.8 字體過長顯示形式
  1.9 跑馬燈
  1.10 設(shè)置圓角
  1.11 首行縮進(jìn)
  1.12 局部改變顏色
  1.13 自適應(yīng)標(biāo)簽云
  1.14 遍歷字體庫
1.1 UILabel被砍頭

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 14)];

label.text = @"設(shè)置高度為14嘶居,看實(shí)際打印高度";

CGSize size = [label.text sizeWithAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];

NSLog(@"高度:%f",size.height);

高度:16.707031


1.2 設(shè)置行間距

UILabel *label = [[UILabel alloc] initWithFrame:
                                  CGRectMake(0, 100, 320, 200)];

[label setBackgroundColor:[UIColor blackColor]];

[label setTextColor:[UIColor whiteColor]];

[label setNumberOfLines:0];

NSString *labelText = @"可以自己按照寬高,字體大小,來計算有多少行邮屁。";

NSMutableAttributedString *attributedString = [[NSMutableAttributedString 
alloc] initWithString:labelText];

NSMutableParagraphStyle *paragraphStyle = 
[[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:5];//調(diào)整行間距

[attributedString addAttribute:NSParagraphStyleAttributeName 
                         value:paragraphStyle 
                         range:NSMakeRange(0, [labelText length])];

label.attributedText = attributedString;

[self.view addSubview:label];

[label sizeToFit];

1.3 設(shè)置刪除線

UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 20)];

NSString *priceString =@"$88.88";
    
NSAttributedString *attrStr = [[NSAttributedString alloc]init
WithString:priceString attributes: @{  
              NSFontAttributeName:[UIFont systemFontOfSize:20.f], 
   NSForegroundColorAttributeName:[UIColor orangeColor], 
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|
                                    NSUnderlinePatternSolid),
 NSStrikethroughColorAttributeName:[UIColor blackColor]
                                                                               }];
priceLabel.attributedText = attrStr;
    
[self.view addSubview:priceLabel];
  
1.4 寬固定整袁,字縮小

priceLabel.adjustsLetterSpacingToFitWidth = NO;

1.5 寬固定,高增加

UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)];

textLabel.backgroundColor = [UIColor lightTextColor];

[textLabel setNumberOfLines:0];

textLabel.lineBreakMode = NSLineBreakByWordWrapping;

textLabel.font = [UIFont fontWithName:@"Arial" size:15];

CGSize size = CGSizeMake(100, 1000);

textLabel.text = @"UILable是iPhone界面最基本的控件佑吝,主要用來顯示文本信息坐昙。";

CGSize msgSie = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:15] 

constrainedToSize:size];

[textLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];

[self.view addSubview:textLabel];

NSLog(@"%f",textLabel.frame.size.height);

1.6 字體顏色漸變

UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon.png"]];

NSString *title = @"Setting";

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];

titleLabel.textColor = titleColor;

titleLabel.text = title;

titleLabel.font = [UIFont boldSystemFontOfSize:20];

titleLabel.backgroundColor = [UIColor clearColor];

[self.view addSubview:titleLabel];
1.7 加邊框

titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; 

titleLabel.layer.borderWidth = 2;
1.8 字體過長顯示形式

例如:UILable是iPhone界面最基本的控件,主要用來顯示文本信息芋忿。


label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯示炸客,后面部分省略不顯示。

label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內(nèi)容長度戈钢,后半部分被刪除痹仙。

label.lineBreakMode = NSLineBreakByTruncatingHead; 例如:...信息。

label.lineBreakMode = NSLineBreakByTruncatingMiddle; 例如:UILable...信息殉了。

label.lineBreakMode = NSLineBreakByTruncatingTail; 例如:UILable是iPhone界面最基本的控件...

label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯示开仰,后面部分省略不顯示。
1.9 跑馬燈

UILabel *runLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,200, 300, 100)];

runLabel.text =@"嚕啦啦嚕啦啦嚕啦嚕啦嚕薪铜,嚕啦嚕啦嚕啦嚕啦嚕啦嚕~~~";

[self.view addSubview:runLabel];

CGRect frame = runLabel.frame;

frame.origin.x = -180;

runLabel.frame = frame;

[UIView beginAnimations:@"testAnimation"context:NULL];

[UIView setAnimationDuration:5.8f];

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationDelegate:self];

[UIView setAnimationRepeatAutoreverses:NO];

[UIView setAnimationRepeatCount:999999];

frame = runLabel.frame;

frame.origin.x =350;

runLabel.frame = frame;

[UIView commitAnimations];
1.10 設(shè)置圓角

UILabel *cornerLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

cornerLabel.layer.cornerRadius = 50;

cornerLabel.clipsToBounds = YES;

cornerLabel.text = @"檸檬";

cornerLabel.textAlignment = NSTextAlignmentCenter;

cornerLabel.backgroundColor =[UIColor redColor];

cornerLabel.textColor = [UIColor whiteColor];

[self.view addSubview:cornerLabel];
1.11 首行縮進(jìn)

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

// 首行縮進(jìn)
style.firstLineHeadIndent = 50.0f;

// 頭部縮進(jìn) 
//style.headIndent = 100;

// 尾部縮進(jìn) 
//style.tailIndent = -10.0f;

NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"演示首行縮進(jìn)" attributes:@{ NSParagraphStyleAttributeName : style}];

UILabel *IndentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; IndentLabel.font = [UIFont systemFontOfSize:17];

IndentLabel.backgroundColor = [UIColor redColor];

IndentLabel.attributedText = attrText;

[self.view addSubview:IndentLabel];
1.12 局部改變顏色

示例:我本月提出反饋 22 個!


NSString *bugCount = @"我本月提出反饋 22 個!";

NSMutableAttributedString *bugCountstr = [[NSMutableAttributedString alloc]initWithString:bugCount];

//設(shè)置:在0-3個單位長度內(nèi)的內(nèi)容顯示成紅色

[bugCountstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 2)];

UILabel *bugLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 20)];

bugLabel.attributedText =bugCountstr;

[self.view addSubview:bugLabel];
1.13 自適應(yīng)標(biāo)簽云

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    CGFloat screen_Width = [UIScreen mainScreen].bounds.size.width;
    
    NSArray *dataArray = @[@"從什么都沒有的地方",@"到什么都沒有的地方",@"我們像沒發(fā)生事一樣",@"自顧地",@"走在路上",@"忘掉了的人只是泡沫",@"用雙手輕輕一觸就破",@"泛黃",@"有他泛黃的理由",@"思念將",@"越來越薄"];
    
    int k = 0;
    for (int i=0; i < dataArray.count; i++)
    {
        
        CGSize size=[[dataArray objectAtIndex:i] boundingRectWithSize:CGSizeMake(150, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
        
        double w = size.width;
        double h = 30;
        double x = 10;
        double y = 64;
        if (i==0)
        {
            x = 20;
            y = 64;
            
        }else{
            
            UILabel *titleLabel = (UILabel *)[self.view viewWithTag:100+i-1];
            if (titleLabel.frame.origin.x+titleLabel.frame.size.width+10+w <= screen_Width)
            {
                x = titleLabel.frame.origin.x+titleLabel.frame.size.width+20;
                y = 64+(h+10)*k;
            }
            else
            {
                k++;
                x = 20;
                y = 64+(h+10)*k;
            }
        }
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
        
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.text = dataArray[i];
        titleLabel.numberOfLines = 0;
        titleLabel.userInteractionEnabled = YES;
        titleLabel.textColor = [UIColor purpleColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchClick:)];
        [titleLabel addGestureRecognizer:tap];
        titleLabel.tag = 100+i;
        [self.view addSubview:titleLabel];
        
    }
    
}

-(void)searchClick:(UITapGestureRecognizer *)tap
{
    
    NSLog(@"%@",((UILabel *)tap.view).text);
    
}

1.14 遍歷字體庫

    NSArray *names = [UIFont familyNames];
    for(NSString *name in names)
        
    {
        
        NSLog(@"%@",name);
    }
    
    // 2.設(shè)置字體字號
    
    label.font = [UIFont fontWithName:@"Georgia" size:20];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抖所,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子痕囱,更是在濱河造成了極大的恐慌,老刑警劉巖暴匠,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鞍恢,死亡現(xiàn)場離奇詭異,居然都是意外死亡每窖,警方通過查閱死者的電腦和手機(jī)帮掉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來窒典,“玉大人蟆炊,你說我怎么就攤上這事∑僦荆” “怎么了涩搓?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長劈猪。 經(jīng)常有香客問我昧甘,道長,這世上最難降的妖魔是什么战得? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任充边,我火速辦了婚禮,結(jié)果婚禮上常侦,老公的妹妹穿的比我還像新娘浇冰。我一直安慰自己贬媒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布肘习。 她就那樣靜靜地躺著际乘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪井厌。 梳的紋絲不亂的頭發(fā)上蚓庭,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機(jī)與錄音仅仆,去河邊找鬼器赞。 笑死,一個胖子當(dāng)著我的面吹牛墓拜,可吹牛的內(nèi)容都是我干的港柜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼咳榜,長吁一口氣:“原來是場噩夢啊……” “哼夏醉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起涌韩,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤畔柔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后臣樱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體靶擦,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年雇毫,在試婚紗的時候發(fā)現(xiàn)自己被綠了玄捕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡棚放,死狀恐怖枚粘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情飘蚯,我是刑警寧澤馍迄,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站局骤,受9級特大地震影響柬姚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庄涡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一量承、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦撕捍、人聲如沸拿穴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽默色。三九已至,卻和暖如春狮腿,著一層夾襖步出監(jiān)牢的瞬間腿宰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工缘厢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留吃度,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓贴硫,卻偏偏與公主長得像椿每,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子英遭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 項目中的要求總是多種多樣的.最近公司項目有新的要求.舉個例子來說,UIlabel在5s上的字體是16號,正常情況下...
    iOS_小紳士閱讀 1,394評論 0 5
  • 一周過去的可真快凹浠ぁ!在這一周里我過得又充實(shí)挖诸,又快了汁尺,而且還收獲滿滿,也認(rèn)識了許多好朋友多律。 每天早上起來我第一...
    張亞霖閱讀 213評論 0 0
  • 今天不是你真正的生日痴突,依然祝你生日快樂,每天關(guān)注你在聽的歌曲菱涤,雖然你已經(jīng)將我加入黑名單,今天你一直在聽趙雷洛勉,也許人...
    窩窩_de93閱讀 247評論 0 0