随手笔记 UIlabel 简单富文本

来源:互联网 发布:centos 6.7 升级内核 编辑:程序博客网 时间:2024/04/28 05:36

 NSString *str1 =@"简介:";

   NSString *str2 = introduction;

    

   NSString *text = [NSStringstringWithFormat:@"%@%@",

                      str1,

                      introduction];

    

    // If attributed text is supported (iOS6+)

   if ([self.videoInfoLBrespondsToSelector:@selector(setAttributedText:)]) {

        

        // Define general attributes for the entire text

       NSDictionary *attribs = @{

                                 NSForegroundColorAttributeName: self.videoInfoLB.textColor,

                                 NSFontAttributeName: self.videoInfoLB.font

                                 };

       NSMutableAttributedString *attributedText =

        [[NSMutableAttributedStringalloc] initWithString:text

                                              attributes:attribs];

        

        // Red text attributes

       UIColor *redColor = [UIColorblackColor];

       UIFont *font=[UIFontsystemFontOfSize:17];

        NSRange redTextRange = [textrangeOfString:str1];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration

        [attributedText setAttributes:@{NSForegroundColorAttributeName:redColor,NSFontAttributeName:font}

                               range:redTextRange];

        

        // Green text attributes

       UIColor *greenColor = [UIColorblackColor];

        NSRange greenTextRange = [textrangeOfString:str2];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration

        [attributedTextsetAttributes:@{NSForegroundColorAttributeName:greenColor}

                               range:greenTextRange];

        

        // Purple and bold text attributes

        

        

       self.videoInfoLB.attributedText = attributedText;

    }

    // If attributed text is NOT supported (iOS5-)

   else {

       self.videoInfoLB.text = text;

    }



0 0
原创粉丝点击