NSMutableAttributedString拼接

UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 300)] ;
    [self.view addSubview:lable] ;


    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@""];
    NSMutableAttributedString *attrStr2 = [[NSMutableAttributedString alloc] initWithString:@"你好,是我"];
    NSMutableAttributedString *attrStr3 = [[NSMutableAttributedString alloc] initWithString:@"刘国彦"];
    NSDictionary *attributeDict = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0],
                                            NSForegroundColorAttributeName: [UIColor redColor]};
    NSDictionary *attributeDict2 = @{NSFontAttributeName: [UIFont systemFontOfSize:12.0],
                                            NSForegroundColorAttributeName: [UIColor yellowColor]};

    [attrStr2 setAttributes:attributeDict range:NSMakeRange(0, attrStr2.length)] ;
    [attrStr3 setAttributes:attributeDict2 range:NSMakeRange(0, attrStr3.length)] ;


    [attrStr appendAttributedString:attrStr2] ;
    [attrStr appendAttributedString:attrStr3] ;


    lable.attributedText  = attrStr ;

暂无评论

相关推荐

获取UIImage的尺寸

int contentWid = self.view.width ; unsigned long width = CGImageGetWidth(image.CGImage) ; unsigned long height …

微信扫一扫,分享到朋友圈

NSMutableAttributedString拼接