3. UILable 的使用

1.  坐标系

来自:http://www.cnblogs.com/mcj-coding/p/5100455.html  QQ: 863740091

如果在平面坐标系中我们要确定一个东西的位置和大小需要什么?(xy值和宽高)

在iOS 程序里,X轴和我们知道的一样,但是Y轴和我们知道的正好反过来 ,也就是说你以前认为的Y轴的负值,在我们iOS中是正数

 

2.  UILabel

展示一下UILabel的使用环境

UILabel 是一个视图类,即UIView的子类,在IOS中所有看的见,摸的着的,都是UIView的子类

 

UILabel 是标签视图,用于显示文字信息。

#import "RootViewController.h"

@interface RootViewController ()

{

    UILabel *_label;

}

@end

 

@implementation RootViewController

 

// 这个ViewController 东西已经准备好,可以添加UI控件了

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 给界面添加了一个label对象

//    [self addLabel];

//    

//    // 修改了label里面的文本信息

//    [self addMetch];

    

//    [self addLabelWithAttr];

 

    // UILable去适应文本的大小

    [self adutText];

}

 

- (void)addLabel

{

    _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];

  // 设置背景颜色

    _label.backgroundColor = [UIColor redColor];

// 设置label 的文本信息

    _label.text = @"yes,you are a bad man, I am a good man. Do you belieffffve it?";

    

    // 字体大小设置

    _label.font = [UIFont systemFontOfSize:30];

    // 设置字体为粗体

    _label.font = [UIFont boldSystemFontOfSize:30];

    // 设置字体为斜体(不支持中文)

    _label.font = [UIFont italicSystemFontOfSize:30];

    

    // 设置字体的样式

    //    NSLog(@"%@",[UIFont familyNames]);(可以打印出字体的样式有多少)

    //    label.font = [UIFont fontWithName:@"Zapfino" size:20];

    

    // 设置自适应宽度

    //    label.adjustsFontSizeToFitWidth = YES;

    

    // 设置文字对齐方式(三种)

    _label.textAlignment = NSTextAlignmentCenter;

    

    // 设置最多显示几行

    _label.numberOfLines = 4;

    

    // 设置中断模式

    //    NSLineBreakByWordWrapping  // 单词换行

    //    NSLineBreakByCharWrapping, // 字符换行 ,如果显示不下,字符作为省略单位     NSLineBreakByClipping, // 单词换行,如果显示不下,字符作为省略单位

    //    NSLineBreakByTruncatingHead,// 单词换行,如果显示不下,省略最后一行行首

    //    NSLineBreakByTruncatingTail,// 单词换行,如果显示不下,省略最后一行行尾

    //    NSLineBreakByTruncatingMiddle // 单词换行,如果显示不下,省略最后一行行中

    _label.lineBreakMode = NSLineBreakByTruncatingMiddle;

    

    // 设置阴影偏移量

    _label.shadowOffset = CGSizeMake(2, 2);

    _label.shadowColor = [UIColor yellowColor];

    

    // 设置透明度(0 ~ 1) 默认是1 数值越小透明度越大

    _label.alpha = 1;

    

    // tag(可以通过这个tag 在其他方法里找到这个控件)

    _label.tag = 100;

 

    // 把label 添加到屏幕上

    [self.view addSubview:_label];

}

 

- (void)addLabelWithAttr

{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 50)];

    

    // 拓展

    NSString *strV = @"SaLaHeiYou,KuNiXiWa";

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

    

    // 修改颜色

    // NSMakeRange(0, 4) 从哪个位置开始,数几个字符,设置他们的颜色

    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 4)];

    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 2)];

    [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(8, 10)];

    

    NSLog(@"%@",[UIFont familyNames]);

    //Zapfino Menlo Baskerville Verdana

    // 修改字体

    [attributeString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Zapfino" size:20] range:NSMakeRange(0, 4)];

    [attributeString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Menlo" size:30] range:NSMakeRange(5, 4)];

    [attributeString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Baskerville" size:10] range:NSMakeRange(10, 4)];

    

    label.attributedText = attributeString;

    [self.view addSubview:label];

 

}

- (void)adutText

{

    // 获取这些文本占用多大的空间

    NSString *str = @"获取这些文本占用多大的空间 获取这些文本占用多大的空间 获取这些文本占用多大的空间";

    

    // 根据文字的大小获取多大的label 可以显示

   CGRect rect = [str boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]} context:nil];

     CGFloat labelH = rect.size.height;

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

    // 0 的时候有多少行 显示多少行

    label.numberOfLines = 0;

    label.font = [UIFont systemFontOfSize:30];

    label.backgroundColor = [UIColor redColor];

    label.text = str;

    [self.view addSubview:label];

}

 

- (void)addMetch

{

  UILabel *label = (UILabel *)[self.view viewWithTag:100];

    label.text = @"北京真好,吸雾霾不用交税";

}

 

 

posted @ 2016-01-04 22:15  吃肉的核桃  阅读(216)  评论(0编辑  收藏  举报