与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];
结果:
不是很好。
我该如何改进?
答案 0 :(得分:5)
您应该将UITextView
与属性字符串键NSLinkAttributeName
一起使用,并使用相应的UITextView
代表处理每个名称。
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)];
[str addAttribute: NSLinkAttributeName value:"a custom url scheme" range:NSMakeRange(range.location + 4, range.length)];
}
然后用这个方法处理:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([[URL scheme] isEqualToString:@"myurl"]) {
// Handle tap
return NO;
}
return YES;
}
答案 1 :(得分:2)
你可以使用这个。你看过https://github.com/venmo/VENTokenField了吗?