我需要从收到的电子邮件数组中创建一个如下所示的 View ,该 View 也具有删除选项。
类似于 gmail 的邮件收件人的东西,除了我的 this 应该是 ScrollView 。我的主要问题是使用根据电子邮件长度延伸的删除按钮创建背景。我目前的方法是使用 3 张图片,一张作为开始,一张作为结尾,带有删除按钮,一张一般用于中间会拉伸(stretch)。有没有其他或更好的方法来做到这一点?
注意:需要iOS 5及以上的支持
最佳答案
给你,我创建了一个SuperLabel
对你来说虽然它可能需要一些调整..但它肯定会帮助你..SuperLabel.h
#import <UIKit/UIKit.h>
@interface SuperLabel : UIView
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title;
@end
SuperLabel.m
#import "SuperLabel.h"
#define MAX_HEIGHT 25.0
@implementation SuperLabel
- (id)initWithFrame:(CGRect)frame andTitle:(NSString *)title
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//Design your label view
self.backgroundColor=[UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1.0];
self.layer.borderColor=[[UIColor orangeColor] CGColor];
self.layer.borderWidth=1.0;
self.layer.cornerRadius=5.0;
//Add a Label
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 100.0, MAX_HEIGHT)];
label.font=[UIFont systemFontOfSize:12.0];
label.textColor=[UIColor grayColor];
label.text=title;
[label sizeToFit];
[self addSubview:label];
//We will get resized frame after using sizeToFit after setting the text in the label.
CGRect rect=label.frame;
//Add a button
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"x" forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont systemFontOfSize:12.0];
btn.titleLabel.textColor=[UIColor grayColor];
rect.origin.x=rect.origin.x+rect.size.width;
rect.origin.y=0;
rect.size=CGSizeMake(25.0, MAX_HEIGHT);
[self addSubview:btn];
[btn addTarget:self action:@selector(deleteMe:) forControlEvents:UIControlEventTouchDragInside];
btn.frame=rect;
//Change the whole frame of the label view
frame.size.height=MAX_HEIGHT;
frame.size.width=btn.frame.origin.x+btn.frame.size.width;
self.frame=frame;
}
return self;
}
-(IBAction)deleteMe:(id)sender{
[self removeFromSuperview];
}
@end
最后使用以下代码添加到您的 View 中。
SuperLabel *label=[[SuperLabel alloc] initWithFrame:CGRectZero andTitle:@"myemail@gmail.com"];
[self.view addSubview:label];
干杯..
关于ios - 如何在 iOS 中创建 gmail 收件人格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469677/