首页 > 技术文章 > UILabel

chunji 2016-03-09 11:24 原文

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

- (void)dealloc

{

    self.window = nil;

    [super dealloc];

}

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    self.window.rootViewController = [[UIViewController alloc] init];

    

    //创建一个label

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

    

    label.backgroundColor = [UIColor lightGrayColor];

    //根据宽度调整字体大小

    //让文字大小自动适应label

    label.adjustsFontSizeToFitWidth = YES;

    //设置文本

    label.text = @"I'm a label ssssssssss";

    //设置系统字体的大小  默认大小 17 磅 pi

    label.font = [UIFont systemFontOfSize:34];

    //获取系统的字体  Helvetica Neue

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

    //根据字体名字设置字体

    label.font = [UIFont fontWithName:[UIFont familyNames][74] size:34];

    //设置字体为粗体      xingkai.ttf  2,3m

    label.font = [UIFont boldSystemFontOfSize:34];

    //设置字体为斜体

    label.font = [UIFont italicSystemFontOfSize:34];

    //NSString

    //attributedString  富文本

    //设置字体的颜色

    label.textColor = [UIColor redColor];

    //文字的阴影

    label.shadowColor = [UIColor blackColor];

    //文字阴影的方向和长度

    label.shadowOffset = CGSizeMake(3, 3);

    //    label.layer.shadowOpacity = 1;

    //设置文字高亮颜色

    label.highlightedTextColor = [UIColor greenColor];

    //设置高亮状态

    label.highlighted = YES;

    

    //设置文字的对齐方式

    // NSTextAlignmentCenter        居中

    // NSTextAlignmentRight         居右

    label.textAlignment = NSTextAlignmentRight;

    [self.window addSubview:label];

    [label release];

    

    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 350, 200)];

    label2.backgroundColor = [UIColor lightGrayColor];

    label2.text = @"Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.";

    //设置行数 默认 1行  0 表示任意行

    label2.numberOfLines = 0;

    //设置折行模式  以什么方式进行换行

    //NSLineBreakByWordWrapping 以单词为隔断

    //NSLineBreakByCharWrapping 以字符为隔断

    label2.lineBreakMode = NSLineBreakByWordWrapping;

    //自适应label大小

    //    [label2 sizeToFit];

    [self.window addSubview:label2];

    [label2 release];

    

    

    NSString *string = @"Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.";

    //(height   1024*8) =

    //根据文字内容计算大小

    // 第1个参数 设置上限的尺寸

    // 第2个参数 换行设置

    // 第3个参数 设置字体  NSFontAttributeName

    // 第4个参数 预留  nil

    CGRect rect = [string boundingRectWithSize:CGSizeMake(350, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];

    NSLog(@"%@", NSStringFromCGRect(rect));

    label2.text = string;

    CGRect labelRect = label2.frame;

    labelRect.size.width = rect.size.width;

    labelRect.size.height = rect.size.height;

    label2.frame = labelRect;

    

    

    return YES;

}

 

推荐阅读