2012-05-11 104 views
0

我正在創建一個聊天應用程序。當消息到達時,它必須說明是誰發送的。例如,如果我是發件人,那麼它應該說在一個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

您應該使用兩個標籤。使用所需顏色(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/

也許以後你會想在你的聊天使用更復雜的邏輯就像名字突出了消息,或者一些其他豐富的文本格式化。你可以用核心文本完成所有這些。

相關問題