import “AppDelegate.h”
@interface AppDelegate ()
{
UILabel *label;
}
- (void)setLink;
@end
@implementation AppDelegate
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
//获取屏幕大小
UIScreen *screen = [UIScreen mainScreen];
self.window = [[UIWindow alloc]initWithFrame:screen.bounds];
self.window.backgroundColor = [UIColor cyanColor];//创建label对象
label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @”【此刻,你愿为这两个字转发传递吗?老兵!】今年,#抗战胜利70周年#。那个年代,中国军队浴血抗击日寇。老兵!用血肉抵挡敌人,如今,风烛残年,满身伤疤,老兵说,早把生死置之度外,只希望人们能记起我们[泪]此刻,请一起做出心形手势,上传照片,用行动倡议关爱老兵,传递下去!为老兵!��”;
//对齐方式
label.textAlignment = NSTextAlignmentCenter;
//文本颜色
label.textColor = [UIColor redColor];
//字体大小
label.font = [UIFont systemFontOfSize:20];
label.font = [UIFont fontWithName:@”Euphemia UCAS” size:20];
label.font = [UIFont boldSystemFontOfSize:20];
NSArray *familyNames = [UIFont familyNames];
NSLog(@”%@”,familyNames);//在label中使文本换行
label.numberOfLines = 0 ;//0不代表限制label的行数label.lineBreakMode = NSLineBreakByTruncatingMiddle;
label.backgroundColor = [UIColor greenColor];
[self.window addSubview:label];
//根据文本的内容的长度来确定label的宽度
CGRect rect = [label.text boundingRectWithSize:CGSizeMake(screen.bounds.size.width, 300) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.frame = CGRectMake(0, 20, rect.size.width, rect.size.height);//
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
NSString *text = @”黑凤梨&&喜欢你”;
label1.text = text;
label1.font = [UIFont systemFontOfSize:40];
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName:label1.font}];
label1.frame = CGRectMake(50, 300, size.width, size.height);
label1.backgroundColor = [UIColor yellowColor];
[self.window addSubview:label1];
/*
在IOS7之后的使用textkit框架中NSMutableAttributedString类,为Label作图文混排,在IOS7之前使用的CoreText框架为Label作图文混排
*/
[self setLink];
[self.window makeKeyAndVisible];return YES;
}
//此方法可以将所选的字链接到百度(void)setLink{
NSMutableAttributedString *mStr = [label.attributedText mutableCopy];
NSURL *url = [NSURL URLWithString:@”http://www.baidu.com“];
[mStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, 17)];
[mStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 17)];label.attributedText = [mStr copy];
}
(void)applicationWillResignActive:(UIApplication *)application {
// 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.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}(void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}(void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}(void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}(void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end