2012-05-11 8 views
0

私はチャットアプリケーションを作っています。メッセージが到着すると誰がそれを送信したかを伝える必要があります。私は、送信者であれば例えば、それは1つのUITextFieldにテキストを設定する

武を言う必要があります。メッセージ

アンドレス:メッセージ

私は「武」と「アンドレスは、」色の異なるが、同じ行になりたいですメッセージと、行の折り返しで、次のように:

Takeshi: some message text, text, text , text 
     text, text, text end. 

私は次のことをしようとしています:

  • メッセージのサイズを計算します。
  • 送信者の名前のサイズを計算します。
  • ""の文字列を送信者の名前の長さで作成します。

"しかし、それだけでは不十分です。

私はこれます

NSString *from; 

if(usr){ 
    from = [NSString stringWithFormat:@"%@: ", @"Yo"]; 
}else{ 
    from = [NSString stringWithFormat:@"%@: ", [_lbSupportName text]]; 
} 
//This give me one string of n characters of ' ' character. 


NSString *spaces = [ChatView stringWithRepeatCharacter:' ' times:from.length * 2]; 

NSString *mensaje = [NSString stringWithFormat:@"%@ %@", spaces, msg]; 



//Calculating the height 
CGSize messageSize = [mensaje sizeWithFont:[UIFont fontWithName:DEFAULT_TEXTFONT size:20] 
          constrainedToSize:CGSizeMake(_scvConversation.frame.size.width - 10, FLT_MAX) 
           lineBreakMode:UILineBreakModeWordWrap]; 

CGSize fromSize = [from sizeWithFont:[UIFont fontWithName:DEFAULT_TEXTFONT size:22] 
        constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX) 
         lineBreakMode:UILineBreakModeWordWrap]; 


//Image Background 
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(5, yConversationPosition + 5, _scvConversation.frame.size.width - 10, messageSize.height + 30)]; 

yConversationPosition += image.frame.size.height + 5; 



//Create the Label From 
UILabel *lb_From = [[UILabel alloc] initWithFrame:CGRectMake(0, 9, fromSize.width, 30)]; 
[lb_From setFont:[UIFont fontWithName:DEFAULT_TEXTFONT size:20]]; 
[lb_From setBackgroundColor:[UIColor clearColor]]; 
[lb_From setText:from]; 



//Create the Label Message 
UILabel *lb_WriteMessage = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, image.frame.size.width-10, image.frame.size.height - 10)]; 
[lb_WriteMessage setNumberOfLines:messageSize.height/DEFAULT_TEXTSIZE]; 
[lb_WriteMessage setFont:[UIFont fontWithName:DEFAULT_TEXTFONT size:20]]; 
[lb_WriteMessage setText:mensaje]; 
[lb_WriteMessage setBackgroundColor:[UIColor clearColor]]; 
[lb_WriteMessage setTextColor:[UIColor colorWithRed:(92/255.0f) green:(98/255.0f) blue:(101/255.0f) alpha:1]]; 



if(usr){ 
    [image setImage:[UIImage imageNamed:@"chat-mensajes-yo.png"]]; 
    [lb_From setTextColor:[UIColor colorWithRed:(230/255.0f) green:(85/255.0f) blue:(84/255.0f) alpha:1]]; 
}else{ 
    [image setImage:[UIImage imageNamed:@"chat-mensajes-asesor.png"]]; 
    [lb_From setTextColor:[UIColor colorWithRed:(28/255.0f) green:(168/255.0f) blue:(175/255.0f) alpha:1]]; 
} 


[lb_WriteMessage addSubview:lb_From]; 
[image addSubview:lb_WriteMessage]; 
[_scvConversation addSubview:image]; 

//Set the contentSize of the scv_Message and scroll to the new Message 
[_scvConversation setContentSize:CGSizeMake(0, yConversationPosition)]; 


if(_scvConversation.contentSize.height > _scvConversation.frame.size.height){ 
    //Scroll to last Message 
    float diference = yConversationPosition - _scvConversation.frame.size.height +5; 
    [_scvConversation setContentOffset:CGPointMake(0, diference)]; 
} 

[image release]; 
[lb_WriteMessage release]; 

を私はそれを行うことができませんでした、助けてください。 :/

答えて

0

2つのラベルを使用してください。 「名前:」の最初のラベルを1行ラベルとして作成し、そのテキストに基づいて幅を計算し、新しいラベルを作成し、その高さと行数を計算して配置します。

[lb_WriteMessage setFrame:CGRectMake(lb_From.frame.origin.x + lb_From.frame.size.width + spacer, lb_from.origin.y, messageSize.width, messageSize.height)]; 

(spacerは、ラベル間に必要なピクセルスペースの数です)。

0

Core Text APIをご覧ください。これは簡単なツールではありませんが、それがどのように機能するかを理解すると信じられないほどの柔軟性が得られます。 ここには良いスタートポイントがあります:http://www.cocoanetics.com/2011/01/befriending-core-text/

おそらく後で、メッセージのハイライト表示や他のリッチテキストの書式設定など、より複雑なロジックをチャットに使用したいと考えています。このすべてをコアテキストで行うことができます。

関連する問題