在一个UITextField中设置文本

问题描述:

我正在创建一个聊天应用程序。当消息到达时,它必须说明是谁发送的。例如,如果我是发件人,那么它应该说在一个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]; 

请帮帮忙,我不能这样做。 :/

您应该使用两个标签。使用所需颜色(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是标签之间想要的像素数量)。

看看Core Text API。这不是一个简单的工具,但是当你了解它的工作原理时,它会给你难以置信的灵活性。 这里是一个很好的起点:http://www.cocoanetics.com/2011/01/befriending-core-text/

也许以后你会想在你的聊天使用更复杂的逻辑就像名字突出了消息,或者一些其他丰富的文本格式化。你可以用核心文本完成所有这些。