2016-08-01 57 views
11

iPhone郵件類似,我想將收件人顯示爲UIButton。但我無法正確實施它。
我正在通過單個UILabel創建所有收件人,然後爲其分配屬性文本。將收件人顯示爲UIButton

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]; 

結果:

enter image description here

不是一個相當不錯的一個。

我該如何改進?

+0

作爲「UIButton」,具有什麼樣的定製/外觀?否則,你可能想使用'NSLinkAttributeName'? – Larme

+0

你也可以使用'UICollectionView'實現同樣的效果,或者你可以使用[github上的TURecipientBar](https://github.com/davbeck/TURecipientBar)。 –

+0

看看這個https://www.cocoacontrols.com/controls/jstokenfield – iPatel

回答

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

不要忘記將textView的「editable」屬性設置爲NO,並將「selectable」設置爲YES,以便名稱可點擊。 – utogaria

+0

這個實現的問題是,當鏈接延伸到兩行時,鏈接文本之間沒有間隙。與我在我的問題中所附的內容非常相似。 – Nitish

3

像什麼@Mohamad謝赫建議,你可以使用帶有屬性串一個UITextView或實際繼承它來創建自己更容易在我看來管理。

如果您不介意使用外部庫,在我的項目之一中,我使用這個podCLTokenInputView。真的很容易使用,並節省我幾個小時的實施我自己的。迅捷版本是here

然後只需按照CLTokenInputViewDelegate協議,並在下面的實現代碼:

func tokenInputView(aView:CLTokenInputView, didAddToken token:CLToken)

func tokenInputView(aView:CLTokenInputView, didRemoveToken token:CLToken)

相關問題