Nit*_*ish 11 iphone objective-c uibutton ios
与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];
Run Code Online (Sandbox Code Playgroud)
结果:
不是很好.
我怎样才能改进它?
您应该使用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)];
}
Run Code Online (Sandbox Code Playgroud)
然后处理这个方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
if ([[URL scheme] isEqualToString:@"myurl"]) {
// Handle tap
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
小智 4
就像在邮件或其他应用程序中一样,我们可以拥有可以区分项目列表的标签功能。以下链接可能会帮助您: https://www.cocoacontrols.com/search?q=tags https://www.cocoacontrols.com/controls/aktagsinputview
归档时间: |
|
查看次数: |
282 次 |
最近记录: |