iPhone邮件类似,我想将收件人显示为UIButton。但是我无法正确实现它。
我在单个UILabel上创建所有收件人,然后将属性文本分配给它。

NSMutableArray *arrRecipients = [NSMutableArray new];

if([message.Recipients containsString:@", "])
{
    NSArray *arr = [message.Recipients componentsSeparatedByString:@", "];

    for(int i = 0; i < arr.count; i++)
    {
        [arrRecipients addObject:[arr objectAtIndex:i]];
    }
}
else
{
    [arrRecipients addObject:message.Recipients];
}

NSString *recipientString = @"";

for(int i = 0; i < arrRecipients.count; i++)
{
    if([recipientString isEqual:@""])
        recipientString = [arrRecipients objectAtIndex:i];
    else
        recipientString = [recipientString stringByAppendingString:[NSString stringWithFormat:@" %@", [arrRecipients objectAtIndex:i]]];

}

NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"to", nil), recipientString]];

for(NSString *value in arrRecipients)
{
    NSRange range = [recipientString rangeOfString:value];
    [str addAttribute:NSBackgroundColorAttributeName value:[UIColor colorWithRed:205.0/255.0 green:205.0/255.0 blue:205.0/255.0 alpha:1.0] range:NSMakeRange(range.location + 4, range.length)];
}

UILabel *recipients = [[UILabel alloc]initWithFrame:CGRectMake(5, subject.frame.origin.y + subject.frame.size.height + 6, viewHeader.frame.size.width - 5, 20)];
recipients.attributedText = str;
recipients.numberOfLines = 0;
recipients.font = [UIFont systemFontOfSize:14];
[viewHeader addSubview:recipients];
[recipients sizeToFit];

[viewHeader sizeToFit];

结果 :

ios - 将收件人显示为UIButton-LMLPHP

不是一个很好的。

我该如何改善呢?

最佳答案

像在邮件或其他应用程序中一样,我们可以使用标签功能来区分项目列表。
以下链接可能会帮助您:
https://www.cocoacontrols.com/search?q=tags
https://www.cocoacontrols.com/controls/aktagsinputview

关于ios - 将收件人显示为UIButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38699023/

10-13 03:44