• NSAttributedString编程




    - (void)viewDidLoad
    {
        [super viewDidLoad];

        
        NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"測试富文本显示"] autorelease];
        //为全部文本设置字体
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24.0] range:NSMakeRange(0, [attributedString length])];
        //将“測试”两字字体颜色设置为蓝色
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 2)];
        //将“富文本”三个字字体颜色设置为红色
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];

        self.label = [[[UILabel alloc] init] autorelease];
        self.label.frame = CGRectMake(10, 100, 300, 30);
        self.label.attributedText = attributedString;
        [self.view addSubview:self.label];
        
        UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
        NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                                                    forKey:NSFontAttributeName];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"用DIC显示文字"
                                                                         attributes:attrsDictionary];
        UILabel *labTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 300, 30)];
        labTwo.attributedText = attrString;
        [self.view addSubview:labTwo];
        [labTwo release];
        [attrString release];
        
        UIFont *dicFont = [UIFont boldSystemFontOfSize:20];
        UIColor *foregroundColor = [UIColor blueColor];
        NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  // 下划线
        
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowBlurRadius = 5;  // 模糊度
        shadow.shadowColor = [UIColor blackColor];
        shadow.shadowOffset = CGSizeMake(1, 3);
        
        NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,
                                   NSUnderlineStyleAttributeName: underline,
                                   NSFontAttributeName:dicFont,
                                   //NSStrokeColorAttributeName:[UIColor redColor], // 绘制空心字颜色
                                   //NSStrokeWidthAttributeName:@3,                  // 默觉得 0。即不改变。正数仅仅改变描边宽度。负数同一时候改变文字的描边和填充宽度。

    比如,对于常见的空心字,这个值通常为3.0。
                                   NSShadowAttributeName:shadow,
                                   NSObliquenessAttributeName:@0.5,   // 设置字体倾斜度
                                   };
        
        NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];
        // NSLog(@"%@", attributedString_str_atts);
        UILabel *labThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 300, 30)];
        labThree.attributedText = attributedString_str_atts;
        [self.view addSubview:labThree];
        [attributedString_str_atts release];
        [labThree release];
        
        UIFont *txtFont = [UIFont boldSystemFontOfSize:30];
        NSDictionary *attDic = @{NSFontAttributeName:txtFont,
                                 NSObliquenessAttributeName:@0.5,
                                 };
        NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:@"測试显示" attributes:attDic];
        
        
        UILabel *labFour = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 300, 50)];
        labFour.attributedText = attribute;
        [self.view addSubview:labFour];
        [attribute release];
        [labFour release];
    }

  • 相关阅读:
    网页的状态掩码
    分享到JavaScript
    右下角收缩广告
    播放列表的收缩展开
    创建文本节点createTextNode
    创建元素节点createElement
    进栈和出栈
    刚刚上班才回来,今天和你说说hash数组
    关于JS中的定时器!!!
    面向对象(程序员最呆的地方,一切皆是对象)
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6908786.html
Copyright © 2020-2023  润新知