博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 第4课 UILabel
阅读量:4008 次
发布时间:2019-05-24

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

0:首先还是通过纯的代码来实现

0:删除3个文件ViewController.hViewController.mMain.storyboard

1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空

2:然后再创建一个MainUIViewController ,它继承自UIViewController

1:AppDelegate.m的didfinshlaunchingwithoptions方法的更新

[csharp]   
 
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];  
  4.     [self.window setRootViewController:[[MyUIViewController alloc] init]];//相当于是android 里面的setcontentview  
  5.     [self.window makeKeyAndVisible];  
  6.       
  7.     return YES;  
  8. }  
2: 
////  MainUiViewController.m//  FourthDemo////  Created by 千雅爸爸 on 16/10/9.//  Copyright © 2016年 kodulf. All rights reserved.//#import "MainUiViewController.h"@interface MainUiViewController ()@end@implementation MainUiViewController- (void)viewDidLoad {    [super viewDidLoad];    [self.view setBackgroundColor:[UIColor whiteColor]];     //使用宏定义来注释掉if 0 endif#if 0    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];        [self.view addSubview:label];        label.text = @"Hello World";    //UILable 默认的就是没有背景色的    label.backgroundColor =[UIColor greenColor];    label.textColor = [UIColor whiteColor];// 字体颜色    label.font = [UIFont systemFontOfSize:20];//字体大小    label.textAlignment = NSTextAlignmentCenter;//居中显示    //查找支持的肢体    NSArray *fontList = [UIFont familyNames];    NSLog(@"字体%@",fontList);    //设置字体和大小    label.font =[UIFont fontWithName:@"Papyrus" size:20];        label.text=@"888888888888888888888/默认的行数为1,如果是0表示无数行;这里是88888888888888888888888888888888888888888888888888888888888";    label.numberOfLines=4;//默认的行数为1,如果是0表示无数行;这里是4行,如果只能显示3行,显示不下了还是使用省略号                //考虑文本的大小根据文本的内容改变++++++开始+++++++    label.numberOfLines = 0;    CGSize maxSize = CGSizeMake(CGRectGetWidth(label.frame), CGFLOAT_MAX);//这里的lable.frame来获取长度,    CGSize textSize = [label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Papyrus" size:20]} context:nil].size;    //NSStringDrawingUsesLineFragmentOrigin如果没有这个选项,可能会导致我们计算出错,这个事必须加入的    //NSStringDrawingUsesFontLeading 考虑行高的计算的    //attributes 是label 原有的属性例如这里是attributes:{NSFontAttributeName:[UIFont fontWithName:@"Papyrus" size:20]}    //context一般都是传nil    //别忘了最后的.size        CGRect frame = label.frame;    frame.size = textSize;//更改size    label.frame = frame;    //考虑文本的大小根据文本的内容改变++++++结束+++++++#endif        //风格化的文本显示内容    NSString *aString = @"iOS";    UILabel *uiLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 375, 45)];    [self.view addSubview:uiLabel];    uiLabel.text=aString;    uiLabel.textAlignment = NSTextAlignmentCenter;    NSLog(@"属性的文字%@",uiLabel.attributedText);        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:aString];//可变的属性后的字符串,    [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:NSMakeRange(0, 1)];//这个太溜了,可以分开来定义字体,和java相比太溜了。    [attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 1)];    [attributeString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(1, 2)];        uiLabel.attributedText = [attributeString copy];//可以直接赋值,但是为了防止误操作,因为attributeString是可变的,而uiLabel.attributedText是不可变的,所以最好是用copy来做        // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

你可能感兴趣的文章
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS AFN 3.0版本前后区别 02
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Android(三) 数据存储之二 SharedPreferences
查看>>
Android(三)数据存储之三SQLite嵌入式数据库
查看>>
Tomcat 6 数据源配置详解
查看>>
『网页制作』页面滚动HTML代码
查看>>
request得到select多选的值
查看>>
WSDL详解
查看>>
struts标签介绍大全
查看>>