1

私は次のようなテキストビューを持っています:

    UITextview staticTxt = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 300, 400)];
        staticTxt.backgroundColor = [UIColor clearColor];
        staticTxt.text = @"this is a textview";
        staticTxt.textColor = [UIColor whiteColor];
[self.view addSubview:staticTxt];

今私が欲しいのは、テキスト「textview」は別の色でなければならないということです。これどうやってするの?ありがとう!!

4

3 に答える 3

1

このクラス メソッドを使用して、コントロール (UILabel、UITextField または UItextView) で異なる色、異なるフォント、および異なるサイズのテキストを使用できます。

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) inputView font:(NSArray*) fontName color:(NSArray*) colorName
{
    inputView.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:14.0].fontName, [UIFont fontWithName:[fontName objectAtIndex:i] size:14.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 expectedinputViewSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:CGSizeMake(186,100) lineBreakMode:UILineBreakModeWordWrap];

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

    [inputView setText:@""];
}

ここで、 text 、その range 、 font 、および color をパラメーターとして渡すことができます。例 :

[CommonFunctions setMultiColorAndFontText:@"This is a textview" rangeString:[NSArray arrayWithObjects:@"This is a",@"textview", nil] textfield:txtEmailAddress font:[NSArray arrayWithObjects:@"Arial",@"Helvetica",nil] color:[NSArray  arrayWithObjects:(UIColor *)[UIColor whiteColor].CGColor,(UIColor *)[UIColor redColor].CGColor,nil]];

異なる色と異なるフォントでテキストを表示します。

注: このメソッドでは、UITextView を使用しました。UILabel または UITextField が必要な場合は、次のように同じ方法で変更するだけです。

UIラベル

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UILabel*) label font:(NSArray*) fontName color:(NSArray*) colorName;

UITextField

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextField*) label font:(NSArray*) fontName color:(NSArray*) colorName;

UITextView

+ (void)setMultiColorAndFontText:(NSString *)text rangeString:(NSArray *)rangeString inputView:(UITextView*) label font:(NSArray*) fontName color:(NSArray*) colorName;

お役に立てば幸いです。

于 2013-04-25T12:17:44.420 に答える
1

この回答で説明されているように、属性付きのテキスト文字列を作成する必要があります。

UITextView 内のテキストに下線を引く

于 2013-04-25T11:08:40.347 に答える