UI-UILabel-attributedText-调研

本文介绍如何使用 iOS 中的 UILabel 控件,并通过 NSMutableAttributedString 设置复杂的文本格式,包括字体、颜色、段落样式等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    

    /*
     1.控件 UIView UILabel UITextField UITextView UIButton
     2.字体、大小、单位、颜色
     */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
    [self.window addSubview:label];
    label.text = @"Label Text Content, This is a text label things attribute";/* 默认为空 */
    label.numberOfLines = 0;
    
    /* 文本的基本数据类型,属性字符串。以此为基础,如果这个设置了相应的属性,则会忽略上面设置的属性,默认为空 */
    NSString *string = label.text;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSUInteger length = [string length];
   
    /* 段落样式 */
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineSpacing = 10;/* 设置行高 */
    style.headIndent = 10;/* 头部缩进 */
    style.lineHeightMultiple = 1.5;/* 行间距 */
    style.alignment = NSTextAlignmentLeft;/* 对齐方式 */
    style.firstLineHeadIndent = 20;/* 首行缩进 */
    style.paragraphSpacing = 20;/* 段落后面的间距 */
    style.paragraphSpacingBefore = 20;/* 段落前面的间距 */
    style.minimumLineHeight = 10;/* 最小行距 */
    style.maximumLineHeight = 30;/* 最大行距 */
    
    
    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
    /* 设置字体 */
    UIFont *baseFont = [UIFont systemFontOfSize:25];
    [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];/* 设置所有的字体 */
    UIFont *boldFont = [UIFont boldSystemFontOfSize:40];
    [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];/* 设置Text这四个字母的字体为粗体 */
    
    /* 设置颜色 */
    UIColor *color = [[UIColor alloc] initWithRed:127/255.0 green:1.0 blue:212/255.0 alpha:1.0];
    [attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Label"]];
    [attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:[string rangeOfString:@"things"]];
    
    
    /* 设置字体 */
    [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AmericanTypewriter" size:18] range:[string rangeOfString:@"things"]];
    [attrString addAttribute:NSObliquenessAttributeName value:@"3" range:[string rangeOfString:@"is"]];
    
    
    /* 字符间距 */
    [attrString addAttribute:NSKernAttributeName value:@8 range:[string rangeOfString:@"attribute"]];
    
    /* 设置文字描边颜色, 宽度 */
    [attrString addAttribute:NSStrokeColorAttributeName value:[UIColor colorWithRed:138/255.0 green:43/255.0 blue:226/255.0 alpha:1.0] range:[string rangeOfString:@"attribute"]];
    [attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"attribute"]];
    
    /* 下划线 */
    [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleDouble) range:[string rangeOfString:@"Content"]];
    [attrString addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:[string rangeOfString:@"Content"]];
    
    [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"This"]];/* 厚的 */
    
    /* 删除线 */
    [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];
    [attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:[string rangeOfString:@"label"]];
    
    label.attributedText = attrString;
    
    label.highlightedTextColor = [UIColor redColor];
    label.highlighted = NO;
    label.enabled = YES;

    
    [attrString release];
    [label release];
    [_window release];
    return YES;
}



效果如⬇️





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值