博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UILabel + 导入字体
阅读量:4696 次
发布时间:2019-06-09

本文共 2000 字,大约阅读时间需要 6 分钟。

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

1.设置文字颜色

  label.textColor = [UIColor orangeColor]; label2.textColor = [UIColor purpleColor];

  lable.textColor = [UIColor colorWithRed:222/255.0 green:59/255.0 blue:17/255.0 alpha:1];

 

2.设置背景颜色

  label.backgroundColor = [UIColor clearColor];

3.设置文字位置

  label.textAlignment = UITextAlignmentLeft; 

4.设置label的行数  0表示多行,自适应

  label.numberOfLines = 2;

5.设置阴影

  label.shadowColor = [UIColor redColor];

  label.shadowOffset = CGSizeMake(1.0,1.0);

6.设置文字过长时的显示格式

  label.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间

  enum {

    UILineBreakModeWordWrap = 0, 

    UILineBreakModeCharacterWrap,

    UILineBreakModeClip,//截去多余部分

    UILineBreakModeHeadTruncation,//截去头部

    UILineBreakModeTailTruncation,//截去尾部

    UILineBreakModeMiddleTruncation,//截去中间

  } UILineBreakMode;

7.导入字体包设置自己的字体

在网上下载好字体文件,拖拽的到工程中,选好target

在plist中加入一行,写入字体文件打开后的名字

lable.font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];

8.计算字符串的长度

  a.确定一个大的容器,width或者height 一定,另一个可变,变化的变量一定要大

  b.确定计算font

  c.boundingRectWithSize

 

    NSString * str = @"The NSString class declares the programmatic interface for an object that manages immutable strings.";

      UIFont * font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];

      NSDictionary * attrDic = @{

NSFontAttributeName:font};

      CGSize bigsize = CGSizeMake(300, 3000);

      CGSize realsize = [str boundingRectWithSize:bigsize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil].size;

 

      UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, realsize.width, realsize.height)];

      label.text = str;

      label.backgroundColor =[UIColor blackColor];

      label.font = [UIFont fontWithName:@"DFGirlW3-B5" size:20];

      label.textColor = [UIColor colorWithRed:222/255.0 green:59/255.0 blue:17/255.0 alpha:1];

      label.textAlignment =NSTextAlignmentLeft;

      label.numberOfLines = 0;

      label.lineBreakMode = NSLineBreakByWordWrapping;

 

 

转载于:https://www.cnblogs.com/huoran1120/p/5138678.html

你可能感兴趣的文章
Windows7 Questions
查看>>
数据库迁移工具
查看>>
不使用中间变量交换两个变量的值
查看>>
Mysql导入sql文件
查看>>
大道至简:软件工程实践者的思想——第六章感想 从编程到工程
查看>>
SharePoint 2010版本表
查看>>
【BootStrap】初步教程
查看>>
[bbk4397] 第1集 - 第一章 AMS介绍
查看>>
Track Active Item in Solution Explorer
查看>>
maven内置属性
查看>>
spring Aop2
查看>>
PHP float加减乘除
查看>>
等差素数列(2017蓝桥杯,二题 )
查看>>
Java开发工程师(Web方向) - 04.Spring框架 - 第5章.Web框架
查看>>
登录窗口抖动效果
查看>>
怎么样才能当老板
查看>>
Tomcat启动时报错:java.net.BindException: Permission denied <null>:80
查看>>
the resource is not on the build path of a Java project报错解决
查看>>
Mysql常用命令行大全
查看>>
深入理解 OUI(Oracle Universal Installer)
查看>>