有关label的学习笔记

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
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值