美文网首页
上下标显示——2020-05-18

上下标显示——2020-05-18

作者: 景彧 | 来源:发表于2020-05-18 12:06 被阅读0次
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cameraTypeCellID = @"typeCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cameraTypeCellID];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cameraTypeCellID];
            
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
            [cell setBackgroundColor:[UIColor whiteColor]];
            [cell.textLabel setFont:[UIFont systemFontOfSize:([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? 22.f : 16.f]];
            [cell.textLabel setNumberOfLines:0];
        }
    
        NSString *cellImageName = nil;
        NSString *cellText = nil;
        
        BOOL     enable = YES;
        if (indexPath.section == 0) { //卡片机
            enable = YES;
            cellImageName = @"MINI_CUBE";
            cellText = DPLocalizedString(@"addcamera_cameratype1", @"AddCamera", nil);
        }
        else if (indexPath.section == 1) { // 摇头机
            enable = YES;
            cellImageName = @"PAN_TILT";
            cellText = DPLocalizedString(@"addcamera_cameratype2", @"AddCamera", nil);
        }
        else if (indexPath.section == 2) { // mini枪机
            enable = YES;
            cellImageName = @"MINI_BULLET";
            cellText = DPLocalizedString(@"addcamera_cameratype3", @"AddCamera", nil);
        }
        else {
      
        }
        
        NSRange range = NSMakeRange(0, 0);
        if ([cellText containsString:@"+"]) {
            range = [cellText rangeOfString:@"+"];
            NSArray *startRanges = @[@(0), @(range.location)];
            NSArray *endRanges = @[@(range.location), @(1)];
            NSString *fontSizeString = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? @"22" : @"16";
            NSArray *fonts = @[fontSizeString, fontSizeString];
            NSArray *colors = @[[UIColor blackColor], [UIColor blackColor]];
            NSArray *baselineOffsets = @[@0, @10, @0, @0];
            
            NSMutableAttributedString * attributedString = [self attributedStringWithString:cellText
                                                                                startRanges:startRanges
                                                                                  endRanges:endRanges
                                                                                      fonts:fonts
                                                                                     colors:colors
                                                                            baselineOffsets:baselineOffsets];
            cell.textLabel.attributedText = attributedString;
            
        }
        else if ([cellText containsString:@"x"]) {
            range = [cellText rangeOfString:@"x"];
            NSArray *startRanges = @[@(0), @(range.location)];
            NSArray *endRanges = @[@(range.location), @(1)];
            NSString *fontSizeString = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? @"22" : @"16";
            NSArray *fonts = @[fontSizeString, fontSizeString];
            NSArray *colors = @[[UIColor blackColor], [UIColor blackColor]];
            NSArray *baselineOffsets = @[@0, @10, @0, @0];
            
            NSMutableAttributedString * attributedString = [self attributedStringWithString:cellText
                                                                                startRanges:startRanges
                                                                                  endRanges:endRanges
                                                                                      fonts:fonts
                                                                                     colors:colors
                                                                            baselineOffsets:baselineOffsets];
            cell.textLabel.attributedText = attributedString;
        }
        else {
            [cell.textLabel setText:cellText];
        }
        
        [cell setUserInteractionEnabled:enable];
        [cell.textLabel setTextColor:RGBACOLOR(87.f, 97.f, 107.f, 1.f)];
        [cell.imageView setImage:[UIImage imageNamed:cellImageName]];
        
        return cell;
    }
    
    #pragma mark - 上下标
    
    - (NSMutableAttributedString *)attributedStringWithString:(NSString *)string
                                                  startRanges:(NSArray *)startRanges
                                                    endRanges:(NSArray *)endRanges
                                                        fonts:(NSArray *)fonts
                                                       colors:(NSArray *)colors
                                               baselineOffsets:(NSArray *)baselineOffsets {
        NSMutableAttributedString * attriString = [[NSMutableAttributedString alloc] initWithString:string];
        
        for ( int i = 0; i < fonts.count; i++ ) {
            UIFont * font = [UIFont systemFontOfSize:10];
            
            if ( [fonts[i] isKindOfClass:[NSString class]] ) {
                NSString * fontStr = fonts[i];
                font = [UIFont systemFontOfSize:fontStr.floatValue];
            }
            else {
                font = fonts[i];
            }
            
            UIColor *color = [UIColor blackColor];
            
            if ( [colors[i] isKindOfClass:[NSString class]] ) {
                NSString * colorStr = colors[i];
                color = [self colorWithHexString:colorStr];
            }
            else {
                color = colors[i];
            }
            
            NSNumber *startIndex = startRanges[i];
            NSNumber *endIndex = endRanges[i];
            NSRange range = NSMakeRange(startIndex.floatValue, endIndex.floatValue);
            NSNumber *baselineOffset = baselineOffsets[i];
            
            [attriString addAttributes:@{
                                         NSFontAttributeName:font,
                                         NSForegroundColorAttributeName:color,
                                         NSBaselineOffsetAttributeName:baselineOffset
                                         }
                                 range:range];
        }
        
        return attriString;
    }
    
    
    - (UIColor *)colorWithHexString:(NSString *)hexString {
        unsigned int red,green,blue;
        NSRange range;
        range.length = 2;
        range.location = 0;
        [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&red];
        range.location = 2;
        [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&green];
        range.location = 4;
        [[NSScanner scannerWithString:[hexString substringWithRange:range]]scanHexInt:&blue];
        return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:1.0f];
    }
    
    选择设备.png

    相关文章

      网友评论

          本文标题:上下标显示——2020-05-18

          本文链接:https://www.haomeiwen.com/subject/fzhuohtx.html