UI基础篇-UILabel

1.控制label里面文字格式的类UIFont

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize
+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize
+ (NSArray *)familyNames//遍历系统字体库  
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize//设置加粗字体
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize//设置斜体字体

2.UILabel的一些属性

字体颜             setTextColor
字阴影颜            setShadowColor
设置文字阴影偏移     setShadowOffset
设置高亮            highlighted
设置高亮颜色         highlightedTextColor
文字对其方式         textAlignment
多行显示 numberOfLines
//=n 说明最多显示n行 =0 行数不限制
折行模式 lineBreakModel
//"NSLineBreakByWordWrapping 以单词为单位进行换行 ,以单词为单位截断
//"NSLineBreakByCharWrapping 以字符为单位进行换行 ,以字符为单位截断
//"NSLineBreakByClipping 以单词为单位进行换行 ,以字符为单位截断
根据文字内容设置frame合适大小 sizeToFit

3.label通过计算文字的size

//通过sizeWithAttributes计算出文字的frame
NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
CGSize textSize = [text sizeWithAttributes:att];
//到最大宽度自动换行,算出行高并设置frame
CGSize maxSize = CGSizeMake(contetnW, MAXFLOAT);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = DChomeDetaillblFont;
CGFloat contentH = [commentData.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
CGRect contentFrame = CGRectMake(contentX, contentY, contetnW, contentH);

4.label富文本

NSString *name = [DCAccountTool account].name;
NSString *prefix = @"发微博";
if(name)
{
    NSString *text = [NSString stringWithFormat:@"%@\n%@",prefix,name];
    //创建一个富文本
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:text];
    //设置一些某些文字的属性
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:[text rangeOfString:prefix]];
    [string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:[text rangeOfString:name]];

    // 创建带有图片的富文本

       // 创建图片
      NSTextAttachment *attch = [[NSTextAttachment alloc] init];
      attch.image = [UIImage imageNamed:@"zan_wupiao"];
      attch.bounds = CGRectMake(0, -2, 12, 12);

      //创建富文本
      NSMutableAttributedString *string = [[NSAttributedString   attributedStringWithAttachment:attch] mutableCopy];
      [string appendAttributedString:str];

    //赋值给label
    lable.attributedText = string;
}

详情见图文混排attributeText.m中

你可能感兴趣的:(UI基础篇-UILabel)