我不喜欢 Three20 在尝试自定义事物、修复分析器警告并将其构建在 Xcode 4 中的糟糕经历。永远不会在新项目中再次使用它。但在这种情况下,它可能会做你想做的事。
但是,您可以相当简单地制作自己的气泡。我希望我能感谢我从中得到这个博客的作者,但那是两年前的事了,现在我在谷歌上找不到它。基本上,您选择一种颜色,绘制圆形端盖,在它们之间放置一个矩形,然后将所需的文本放在顶部。
UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH));
CGContextRef context = UIGraphicsGetCurrentContext();
// the image should have a white background
[[UIColor clearColor] set];
CGRect myRect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);
UIRectFill(myRect);
[self.color set];
// Drawing code
CGSize textSize = [self.text sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];
double capDiameter = textSize.height;
double capRadius = capDiameter / 2.0;
double capPadding = capDiameter / 4.0;
double textWidth = MAX( capDiameter, textSize.width ) ;
CGRect textBounds = CGRectMake(capPadding, 0.0, textWidth, textSize.height);
CGRect badgeBounds = CGRectMake(0.0, 0.0, textWidth + (2.0 * capPadding), textSize.height);
double offsetX = (CGRectGetMaxX(myRect) - CGRectGetMaxX(badgeBounds)) / 2.0;
double offsetY = (CGRectGetMaxY(myRect) - CGRectGetMaxY(badgeBounds)) / 2.0;
badgeBounds = CGRectOffset(badgeBounds, offsetX, offsetY);
textBounds = CGRectOffset(textBounds, offsetX, offsetY);
CGContextFillEllipseInRect(context,
CGRectMake(badgeBounds.origin.x, badgeBounds.origin.y, capDiameter, capDiameter));
CGContextFillEllipseInRect(context,
CGRectMake(badgeBounds.origin.x + badgeBounds.size.width - capDiameter, badgeBounds.origin.y,
capDiameter, capDiameter));
CGContextFillRect(context, CGRectMake(badgeBounds.origin.x + capRadius, badgeBounds.origin.y,
badgeBounds.size.width - capDiameter, capDiameter));
if(self.textColor != nil) {
const CGFloat* colors = CGColorGetComponents(self.textColor.CGColor);
CGColorSpaceRef space = CGColorGetColorSpace(self.textColor.CGColor);
CGColorSpaceModel model = CGColorSpaceGetModel(space);
if(model == kCGColorSpaceModelMonochrome)
// monochrome color space has one grayscale value and one alpha
CGContextSetRGBFillColor(context, *(colors + 0), *(colors + 0), *(colors + 0), *(colors + 1));
else
// else a R,G,B,A scheme is assumed.
CGContextSetRGBFillColor(context, *(colors + 0), *(colors + 1), *(colors + 2), *(colors + 3));
} else
// else use plain white
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
[self.text drawInRect:textBounds
withFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]
lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
您描述的删除按钮行为也非常简单 - 一旦您选择并接受了匹配项,在插入点绘制气泡并将 UITextField 的框架更改为在气泡之后开始。如果您在 UITextField 框架的开头并且您得到另一个删除,请移除气泡 UIImageView,将 UITextField 框架重置为 UIImageView 曾经开始的位置。
或者您可以使用 UIWebView 作为地址输入字段,您可以在其中显示使用代码创建的气泡图像以及文本(请参阅this StackOverflow question),同时进行编辑。您只需要为委托方法 shouldChangeCharactersInRange 编写一个处理程序来处理删除添加的地址和气泡图像,如果气泡是删除操作的目标项。