iPhone:如何以编程方式制作提示气球?

编程入门 行业动态 更新时间:2024-10-24 06:38:46
本文介绍了iPhone:如何以编程方式制作提示气球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请提供代码,像Grindr一样以编程方式制作提示气球.

Please provide the code to make a tip balloon programmatically, like Grindr has.

我希望根据文字&字体大小. 而且,我希望能够更改箭头的位置. 如果在一端,则应为直角三角形.否则,它应该是一个等边三角形.

I want it to be sized automatically, based on the text & font-size. And, I want to be able to change the location of the arrow. If it's on an end, it should be a right triangle. Otherwise, it should be an eqilateral triangle.

推荐答案

// AppDelegate.h @interface AppDelegate : NSObject <UIApplicationDelegate> { } @property (nonatomic, retain) UIWindow *window; @end // AppDelegate.m #import "AppDelegate.h" #import "TipBalloon.h" @implementation AppDelegate @synthesize window; #pragma mark NSObject - (void)dealloc { [window release]; [super dealloc]; } #pragma mark UIApplicationDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Add the tip balloon. TipBalloon *textExample = [[TipBalloon alloc] initAtPoint:CGPointMake(6.0f, 30.0f) withText: @"Hello world! I like to make things. Yay! They are really cool things!"]; [window addSubview:textExample]; [textExample release]; window.backgroundColor = [UIColor brownColor]; [window makeKeyAndVisible]; return YES; } @end // TipBalloon.h @interface TipBalloon : UIView { } @property (nonatomic, copy) NSString *text; - (id)initAtPoint:(CGPoint)point withText:(NSString *)string; - (void)drawOutlineInContext:(CGContextRef)context; - (void)drawTextInContext:(CGContextRef)context; @end // TipBallon.m #import "TipBalloon.h" // TODO make some of these instance variables to add more customization. static const CGFloat kArrowOffset = 0.0f; static const CGFloat kStrokeWidth = 2.0f; static const CGFloat kArrowSize = 14.0f; static const CGFloat kFontSize = 12.0f; static const CGFloat kMaxWidth = 196.0f; static const CGFloat kMaxHeight = CGFLOAT_MAX; static const CGFloat kPaddingWidth = 12.0f; static const CGFloat kPaddingHeight = 10.0f; @implementation TipBalloon @synthesize text; #pragma mark NSObject - (void)dealloc { [text release]; [super dealloc]; } #pragma mark UIView - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef contextRef = UIGraphicsGetCurrentContext(); [self drawOutlineInContext:contextRef]; [self drawTextInContext:contextRef]; } #pragma mark TipBalloon - (id)initAtPoint:(CGPoint)point withText:(NSString *)string { CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:CGSizeMake(kMaxWidth, kMaxHeight) lineBreakMode:UILineBreakModeWordWrap]; CGRect rect = CGRectMake(point.x, point.y, size.width+kPaddingWidth*2.0f, size.height+kPaddingHeight*2.0f+kArrowSize); if ((self = [super initWithFrame:rect])) { self.text = string; UIColor *clearColor = [[UIColor alloc] initWithWhite:0.0f alpha:0.0f]; self.backgroundColor = clearColor; [clearColor release]; } return self; } - (void)drawOutlineInContext:(CGContextRef)context { CGRect rect = self.bounds; rect.origin.x += (kStrokeWidth/2.0f); rect.origin.y += kStrokeWidth + kArrowSize; rect.size.width -= kStrokeWidth; rect.size.height -= kStrokeWidth*1.5f + kArrowSize; CGFloat radius = 11.0f; CGFloat x_left = rect.origin.x; CGFloat x_right = x_left + rect.size.width; CGFloat y_top = rect.origin.y; CGFloat y_bottom = y_top + rect.size.height; CGContextBeginPath(context); CGContextSetLineWidth(context, kStrokeWidth); CGContextSetRGBStrokeColor(context, 0.0f/255.0f, 255.0f/255.0f, 0.0f/255.0f, 1.0f); // green CGContextSetGrayFillColor(context, 1.0f, 1.0f); // white background CGContextMoveToPoint(context, x_left+radius, y_top); CGContextAddLineToPoint(context, x_left+radius+kArrowOffset, y_top); // Draw triangle. // CGContextAddLineToPoint(context, x_left+radius+kArrowOffset+kArrowSize/2.0f, y_top-kArrowSize); CGContextAddLineToPoint(context, x_left+radius+kArrowOffset, y_top-kArrowSize); CGContextAddLineToPoint(context, x_left+radius+kArrowOffset+kArrowSize, y_top); static const CGFloat F_PI = (CGFloat)M_PI; CGContextAddArc(context, x_right-radius, y_top+radius, radius, 3.0f*F_PI/2.0f, 0.0f, 0); CGContextAddArc(context, x_right-radius, y_bottom-radius, radius, 0.0f, F_PI/2.0f, 0); CGContextAddArc(context, x_left+radius, y_bottom-radius, radius, F_PI/2.0f, F_PI, 0); CGContextAddArc(context, x_left+radius, y_top+radius, radius, F_PI, 3.0f*F_PI/2.0f, 0); CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke); } - (void)drawTextInContext:(CGContextRef)context { CGRect rect = self.bounds; rect.origin.x += kPaddingWidth; rect.origin.y += kPaddingHeight + kArrowSize; rect.size.width -= kPaddingWidth*2.0f; rect.size.height -= kPaddingHeight*2.0f; CGContextSetGrayFillColor(context, 0.0f, 1.0f); // black text [text drawInRect:rect withFont:[UIFont systemFontOfSize:kFontSize] lineBreakMode:UILineBreakModeWordWrap]; } @end

更多推荐

iPhone:如何以编程方式制作提示气球?

本文发布于:2023-11-27 20:14:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:气球   提示   方式   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!