-2

ラベルのテキストを設定しようとしています。文ごとに異なるフォントサイズと色を設定したい。言う

#define Label @"This is a label Title. This is label subscript"

ここでは、1 つのフォントで「これはラベルのタイトルです」が必要です。そして「This is label subscript」を他のフォントで。出来ますか。のようにしてみました

#define Label @"This is a label Title. <b>This is label subscript <b>"他にもいくつかあります。しかし、変化はありません。

ありがとうジテン

4

2 に答える 2

0

この方法を使用できます。

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString label:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName{
label.layer.sublayers = nil;

NSMutableAttributedString *mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:text];

for (int i =0 ; i<[rangeString count]; i++)
{
    CTFontRef  ctFont = CTFontCreateWithName((__bridge CFStringRef) [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:10.0].pointSize, NULL);

    NSRange whiteRange = [text rangeOfString:[rangeString objectAtIndex:i]];

    if (whiteRange.location != NSNotFound)
    {
        [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[colorName objectAtIndex:i] range:whiteRange];
        [mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName
                                        value:( __bridge id)ctFont
                                        range:whiteRange];
    }
}

CGSize expectedLabelSize = [text sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];

CATextLayer   *textLayer = [[CATextLayer alloc]init];
textLayer.frame =CGRectMake(0,0, label.frame.size.width,expectedLabelSize.height+4);
textLayer.contentsScale = [[UIScreen mainScreen] scale];
textLayer.string=mutableAttributedString;
textLayer.opacity = 1.0;
textLayer.alignmentMode = kCAAlignmentLeft;
[label.layer addSublayer:textLayer];
[textLayer setWrapped:TRUE];

label.lineBreakMode = UILineBreakModeWordWrap;

[label setText:@""];

}

この方法を使用すると、同じラベルに異なるサイズと異なる色のテキストを表示できます。

于 2013-04-12T11:35:42.360 に答える