Ios 将收件人显示为UIButton
与Ios 将收件人显示为UIButton,ios,objective-c,iphone,uibutton,Ios,Objective C,Iphone,Uibutton,与iPhonemail类似,我希望将收件人显示为ui按钮。但是我不能正确地实施它。 我在一个UILabel上创建所有收件人,然后为其分配属性文本 NSMutableArray *arrRecipients = [NSMutableArray new]; if([message.Recipients containsString:@", "]) { NSArray *arr = [message.Recipients componentsSeparatedByString:@", "];
iPhone
mail类似,我希望将收件人显示为ui按钮
。但是我不能正确地实施它。我在一个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];
NSMutableArray*arrRecipients=[NSMutableArray new];
if([message.Recipients包含字符串:@“,”]))
{
NSArray*arr=[message.Recipients组件由字符串分隔:@“,”;
对于(int i=0;i
结果:
不太好
如何改进它?您应该使用
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;
}
就像在邮件或其他应用程序中一样,我们可以使用标签功能来区分项目列表。 以下链接可能对您有所帮助:
正如@Mohamad Sheikh所建议的,在我看来,您可以使用带有属性字符串的UITextView,或者实际上将其子类化以创建自己的,这样更易于管理 如果您不介意使用外部库,在我的一个项目中,我使用了这个
pod
。它真的很容易使用,并且为我节省了几个小时的实现时间。swift版本是
然后只需遵循CLTokenInputViewDelegate
中的协议,并在以下步骤中实现代码:
func-tokenInputView(aView:CLTokenInputView,didAddToken-token:CLToken)
func tokenInputView(aView:CLTokenInputView,didRemoveToken token:CLToken)
您可以使用此项。你看到了吗 作为“UIButton”,有什么样的定制/外观?否则,您可能需要使用NSLinkAttributeName
?您也可以使用UICollectionView
或使用。请注意,不要忘记将textView的“可编辑”属性设置为否,将“可选”设置为是,以便名称可以单击。此实现的问题是,当链接扩展到两行时,链接文本之间没有间隙。与我在问题中附带的内容非常相似。