iOS 界面上绘制不同字体 颜色 大小的字符串

ios 6.0 及以上版本有系统自带的类实现:

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
  
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
 
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
 
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
 
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
 
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
 
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
   
    lbtext.attributedText = str;
位置不一定定死  可以 依据要修改的文字的查找range;

下面时兼容iOS 6.0 一下的,着需要下载一款第三方类TTTAttributedLabel,导入coretext.framework , 具备之后开始实现:

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:lbtext.frame];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor darkGrayColor];
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.numberOfLines = 3;
    

        NSString *text = @"Lorem ipsum dolar sit amet!LoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLoremLorem";
        [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
            
            NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolar" options:NSCaseInsensitiveSearch];
            NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];
 
            UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
            CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
            CTFontRef fonts = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize+8, NULL);
            if (font) {
                
               [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)fonts range:boldRange];
 
                
                [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:boldRange];
                
                
                [mutableAttributedString addAttribute:(NSString*)kCTFontSizeAttribute value:(__bridge id)fonts range:boldRange];
                

                [mutableAttributedString addAttribute:kTTTStrikeOutAttributeName value:[NSNumber numberWithBool:YES] range:strikeRange];
                
                CFRelease(fonts);
                CFRelease(font);
            }
            
            return mutableAttributedString;
        }];
    
               [self.view addSubview:label];

mark!


你可能感兴趣的:(字体,label)