我正在制作一个聊天应用程序。 当一条消息到达时,它必须说是谁发送的。例如,如果我是发件人,那么它应该说
武志:留言
安德烈斯:留言
我希望"Takeshi"和"Andres"具有不同的颜色,但与消息在同一行,并带有换行,如下所示:
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];
请帮忙,我做不到。
您应该使用两个标签。使用您想要的颜色(1 行标签)为"Name:"创建第一个标签,并根据文本计算其宽度,然后创建一个新标签并计算其高度和行数并使用以下命令定位它:
[lb_WriteMessage setFrame:CGRectMake(lb_From.frame.origin.x + lb_From.frame.size.width + spacer, lb_from.origin.y, messageSize.width, messageSize.height)];
(其中间隔是标签之间所需的像素间距数)。
看看 Core Text API。它不是一个简单的工具,但当您了解它的工作原理时,它会为您提供令人难以置信的灵活性。这是一个很好的起点:http://www.cocoanetics.com/2011/01/befriending-core-text/
可能稍后您会希望在聊天中使用更复杂的逻辑,例如消息中突出显示的名称或其他一些富文本格式。您可以使用核心文本完成所有这些操作。