{"id":644,"date":"2018-02-24T15:54:57","date_gmt":"2018-02-24T07:54:57","guid":{"rendered":"http:\/\/60.205.208.74\/?p=644"},"modified":"2018-02-24T15:54:57","modified_gmt":"2018-02-24T07:54:57","slug":"ios-%e6%b0%94%e6%b3%a1%e8%a7%86%e5%9b%be%e7%9a%84%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=644","title":{"rendered":"iOS \u6c14\u6ce1\u89c6\u56fe\u7684\u65b9\u5f0f"},"content":{"rendered":"<span itemprop=\"description\"><p>\u5728\u4e00\u4e9bios\u5e94\u7528\u4e2d\u7ecf\u5e38\u4f1a\u9700\u8981\u81ea\u5b9a\u4e49\u89c6\u56fe\uff0c\u5176\u4e2d\u6c14\u6ce1\u89c6\u56fe\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>\u65b9\u5f0f\u4e00\uff1a<\/p>\n<p>1\uff1a\u65b0\u5efa\u4e00UIView\u7684\u5b50\u7c7bUIBubbleView<\/p>\n<p>2\uff1a\u5728initwithframe\u4e2d\u521d\u59cb\u5316<\/p>\n<p>a):\u8981\u7ed8\u5236\u6c14\u6ce1\u7684\u5750\u6807targetpoint(x,y);<\/p>\n<p>b):\u6c14\u6ce1\u7684frame bubbleFrame;<\/p>\n<p>c):\u8c03\u7528\u65b9\u6cd5setneedslayout;<\/p>\n<p>3:\u8c03\u7528drawCGRect\u65b9\u6cd5<\/p>\n<p>\u53ea\u8981\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>[sourcecode lang=&#8221;plain&#8221;]<\/p>\n<p>&#8211; (void)drawRect:(CGRect)rect{<\/p>\n<p>\tNSLog(@&quot;bubble draw&quot;);<br \/>\n\tCGContextRef c = UIGraphicsGetCurrentContext();<br \/>\n\tCGContextSetRGBStrokeColor(c, 0.0, 0.0, 0.0, 0.0);<br \/>\n\tCGContextSetLineWidth(c, 3.0);<br \/>\n\t\/\/\u786e\u5b9a\u753b\u7ebf\u7684\u5bbd\u5ea6\uff0c\u5bf9\u8c61\u7ec4\u5408\uff0c\u989c\u8272<br \/>\n\tCGMutablePathRef bubblePath = CGPathCreateMutable();<br \/>\n\t\/\/\u7ed8\u5236\u8d77\u70b9\uff0d\u7bad\u5934\u53f3\u8fb9\uff0d\u6c14\u6ce1\u53f3\u4e0a\u9876\u70b9\uff0d\u53f3\u4e0b\u9876\u70b9\uff0d\u5de6\u4e0b\u9876\u70b9\uff0d\u5de6\u4e0a\u9876\u70b9\uff0d\u7bad\u5934\u5de6\u8fb9\uff0d\u8d77\u70b9\u95ed\u5408<br \/>\n\tCGPathMoveToPoint(bubblePath, NULL, targetPoint.x, targetPoint.y);<br \/>\n\tCGPathAddLineToPoint(bubblePath, NULL, targetPoint.x+pointerSize-1, targetPoint.y-2);<br \/>\n\tCGPathAddArcToPoint(bubblePath, NULL, bubbleFrame.origin.x, bubbleFrame.origin.y,<br \/>\n\t\t\t\t\t\tbubbleFrame.origin.x + bubbleFrame.size.width, bubbleFrame.origin.y, cornerRadius);<br \/>\n\tCGPathAddArcToPoint(bubblePath, NULL, bubbleFrame.origin.x + bubbleFrame.size.width, bubbleFrame.origin.y,<br \/>\n\t\t\t\t\t\tbubbleFrame.origin.x+bubbleFrame.size.width, bubbleFrame.origin.y+bubbleFrame.size.height, cornerRadius);<br \/>\n\tCGPathAddArcToPoint(bubblePath, NULL, bubbleFrame.origin.x+bubbleFrame.size.width, bubbleFrame.origin.y+bubbleFrame.size.height,<br \/>\n\t\t\t\t\t\tbubbleFrame.origin.x, bubbleFrame.origin.y+bubbleFrame.size.height, cornerRadius);<br \/>\n\tCGPathAddArcToPoint(bubblePath, NULL, bubbleFrame.origin.x, bubbleFrame.origin.y+bubbleFrame.size.height,<br \/>\n\t\t\t\t\t\tbubbleFrame.origin.x, targetPoint.y+pointerSize , cornerRadius);<br \/>\n\tCGPathAddLineToPoint(bubblePath, NULL, targetPoint.x + pointerSize, targetPoint.y+pointerSize);<br \/>\n\tCGPathCloseSubpath(bubblePath);<\/p>\n<p>\t\/\/\u7ed8\u5236\u9634\u5f71<br \/>\n\tCGContextAddPath(c, bubblePath);<br \/>\n\tCGContextSaveGState(c);<br \/>\n\tCGContextSetShadow(c, CGSizeMake(0, 3), 5);<br \/>\n\tCGContextSetRGBFillColor(c, 255.0, 255.0, 255.0, 0.0);<br \/>\n\tCGContextFillPath(c);<br \/>\n\tCGContextRestoreGState(c);<\/p>\n<p>\t\/\/\u8bbe\u7f6e\u8fb9\u7ebf\u989c\u8272<br \/>\n\tCGContextAddPath(c, bubblePath);<br \/>\n\tCGContextClip(c);<\/p>\n<p>\tint numberBorderComponents = CGColorGetNumberOfComponents([borderColor CGColor]);<br \/>\n\tconst CGFloat *borderComponents = CGColorGetComponents(borderColor.CGColor);<br \/>\n\tCGFloat r,g,b,a;<br \/>\n\tif (numberBorderComponents == 2) {<br \/>\n\t\tr = borderComponents[0];<br \/>\n\t\tg = borderComponents[0];<br \/>\n\t\tb = borderComponents[0];<br \/>\n\t\ta = borderComponents[1];<br \/>\n\t}else {<br \/>\n\t\tr = borderComponents[0];<br \/>\n\t\tg = borderComponents[1];<br \/>\n\t\tb = borderComponents[2];<br \/>\n\t\ta = borderComponents[3];<br \/>\n\t}<br \/>\n\tCGContextSetRGBStrokeColor(c, r, g, b, a);<br \/>\n\tCGContextAddPath(c, bubblePath);<br \/>\n\tCGContextDrawPath(c, kCGPathStroke);<br \/>\n\tCGPathRelease(bubblePath);<\/p>\n<p>}<\/p>\n<p>[\/sourcecode]<\/p>\n<p>4:\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528UIBubbleView\u5c31\u53ef\u4f7f\u7528\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528UIImage\u7684\u53ef\u5c40\u90e8\u62c9\u4f38\u7684\u529f\u80fd\uff0c\u5bf9\u4e00\u4e2a\u6c14\u6ce1\u56fe\u7247\u8fdb\u884c\u5c40\u90e8\u62c9\u4f38\uff0c\u62c9\u4f38\u540e\u7684\u56fe\u7247\u4f5c\u4e3auiview\u7684\u80cc\u666fview<\/p>\n<p>1\uff1a\u627e\u4e00\u5f20\u6c14\u6ce1\u56fe\u7247\u5982\u4e0b\u56fe\uff1b<\/p>\n<p>2\uff1anew UIView \u00a0,\u6dfb\u52a0\u80cc\u666f\u56feview<\/p>\n<p>\u53ea\u8981\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>[sourcecode lang=&#8221;plain&#8221;]- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {<br \/>\n\t\/\/ build single chat bubble cell with given text<br \/>\n\tUIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];<br \/>\n\treturnView.backgroundColor = [UIColor clearColor];<br \/>\n\t\/\/\u6839\u636e\u6c14\u6ce1\u7bad\u5934\u7684\u65b9\u5411\u9009\u62e9\u4e0d\u540c\u6c14\u6ce1\u56fe\u7247<br \/>\n\tUIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@&quot;bubbleSelf&quot;:@&quot;bubble&quot; ofType:@&quot;png&quot;]];<br \/>\n\t\/\/\u5bf9\u6c14\u6ce1\u56fe\u7247\u8fdb\u884c\u62c9\u4f38<br \/>\n\tUIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]];<\/p>\n<p>\tUIFont *font = [UIFont systemFontOfSize:12];<br \/>\n\t\/\/\u83b7\u53d6\u6587\u5b57\u6240\u5360\u7684\u5927\u5c0f<br \/>\n\tCGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap];<\/p>\n<p>\tUILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width+10, size.height+10)];<br \/>\n\tbubbleText.backgroundColor = [UIColor clearColor];<br \/>\n\tbubbleText.font = font;<br \/>\n\tbubbleText.numberOfLines = 0;<br \/>\n\tbubbleText.lineBreakMode = UILineBreakModeCharacterWrap;<br \/>\n\tbubbleText.text = text;<\/p>\n<p>\tbubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height+40.0f);<br \/>\n\tif(fromSelf)<br \/>\n\t\treturnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height+50.0f);<br \/>\n\telse<br \/>\n\t\treturnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height+50.0f);<\/p>\n<p>\t[returnView addSubview:bubbleImageView];<br \/>\n\t[bubbleImageView release];<br \/>\n\t[returnView addSubview:bubbleText];<br \/>\n\t[bubbleText release];<\/p>\n<p>\treturn [returnView autorelease];<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>3\uff1a\u5728\u9700\u8981\u7684\u5730\u65b9\u8c03\u7528\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e24\u79cd\u65b9\u5f0f\u5404\u6709\u81ea\u5df1\u7684\u65b9\u4fbf\u4e4b\u5904\uff0c\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u9009\u62e9\u4f7f\u7528<\/p>\n<figure style=\"width: 44px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0070\/3691\/1341d3d2-97fd-34de-8d79-64077e729ff2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0070\/3691\/1341d3d2-97fd-34de-8d79-64077e729ff2.png\" alt=\"\u6548\u679c\u56fe\" width=\"44\" height=\"31\" \/><\/a><figcaption class=\"wp-caption-text\">\u6548\u679c\u56fe\uff1a<\/figcaption><\/figure>\n<figure style=\"width: 44px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0070\/3708\/74ab39c2-57ed-348e-a2b8-223e6248893d.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium\" src=\"http:\/\/dl2.iteye.com\/upload\/attachment\/0070\/3708\/74ab39c2-57ed-348e-a2b8-223e6248893d.png\" alt=\"\u6548\u679c\u56fe\" width=\"44\" height=\"31\" \/><\/a><figcaption class=\"wp-caption-text\">\u6548\u679c\u56fe<\/figcaption><\/figure>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u5728\u4e00\u4e9bios\u5e94\u7528\u4e2d\u7ecf\u5e38\u4f1a\u9700\u8981\u81ea\u5b9a\u4e49\u89c6\u56fe\uff0c\u5176\u4e2d\u6c14\u6ce1\u89c6\u56fe\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4e24\u79cd\u65b9\u5f0f\uff1a &#038;n&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=644\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26,12],"tags":[74,93],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","category-blog","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=644"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}