5

私は文字列のようなものを持っています、

NSSting *myString=@"First Second Third Fourth Fifth Sixth Seventh";

この文字列を、フォントの種類と色が異なる単一のラベルで表示したいと考えています。

すなわち、

「最初」の文字列は太字にする必要があります。

「2 番目」の文字列はイタリック体にする必要があります。

「3番目」の文字列は赤色にする必要があります。

「4番目」の文字列は緑色にする必要があります。

「5番目」の文字列は、システム フォントのボールド イタリックにする必要があります。

「6 番目」の文字列は、アリアル ボールドにする必要があります。

「7番目」の文字列は、サイズ 34 の arial bold にする必要があります。

私はstackoverflowでいくつかの回答を調べましたが、それらの回答は明確ではありませんでした..

誰でもいくつかのガイドラインを提供できますか。

前もって感謝します。

解決策:まずNSAttributedString、要件に従って操作します。

NSAttributedStringu を使用するとiOS 6 UILabelhasで実行できますattributedString property

Below ios 6.0にはTTAtributeLabelまたは anythird party attributedLabel を使用しdisplaying attributedStringます。

4

6 に答える 6

3

このコードを使用して、異なる色を異なる単語に設定します..

NSString *test = @"First Second Third Fourth Fifth Sixth Seventh";

 CFStringRef string =  (CFStringRef) test;
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string);



    /*
     Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its
     attributes right when we create it. We can change them if we use mutable form of CFAttributeString.
     */



    //Lets choose the colors we want to have in our string
    CGColorRef _orange=[UIColor orangeColor].CGColor;
    CGColorRef _green=[UIColor greenColor].CGColor;
    CGColorRef _red=[UIColor redColor].CGColor;
    CGColorRef _blue=[UIColor blueColor].CGColor;    




    //Lets have our string with first 20 letters as orange
    //next 20 letters as green
    //next 20 as red
    //last remaining as blue
    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green);
    CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);    
    CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue);

また、次のように Image で Color を設定することもできます。

[yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]];

このリンクから私の別の回答を参照してください

于 2013-05-08T11:41:25.020 に答える
2

TTTAttributedLabelは、iOS < 6 で私のために仕事をしました。

于 2013-05-08T12:14:04.710 に答える
1

iOS 6.0 以降 では、 を受け入れるUILabelプロパティ:があります。
@property(nonatomic,copy) NSAttributedString *attributedText
NSAttributedString

NSAttributedString要件に合った を作成できます。

于 2013-05-08T11:35:39.530 に答える
1

NSAttributeLabelを使用しない場合は、 これを試してください。私にとってはうまく機能します

このメソッドを CommonFuction クラスに追加 & このフレームワークCoreText.frameworkを追加

+ (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.text = @"";
}

そして、このメソッドを次のように呼び出した後:-

[CommonFunctions setMultiColorAndFontText:string rangeString:[NSArray arrayWithObjects:str1,str2,str3,str4,str5,str6, nil] label:yourLabel font:[NSArray arrayWithObjects:@"Arial",@"Arial",@"Arial",@"Arial",@"Arial",@"Arial"nil] color:[NSArray  arrayWithObjects:(UIColor *)[UIColor colorWithRed:(0/255.f) green:(167/255.f) blue:(230/255.f) alpha:1.0f].CGColor,(UIColor *)[UIColor colorWithRed:(189/255.f) green:(189/255.f) blue:(189/255.f) alpha:1.0f].CGColor, color3,color4,color5,color6,nil]];
于 2013-05-08T13:01:14.847 に答える
0

私は開発中に同じ問題に直面しました。色、太字、長さ。すべてのラベルをまとめて追加し、新しい Label オブジェクトを作成します

于 2013-05-08T13:26:19.917 に答える