//
ChatBubble.h
Chatbubble
//
Created by Big Huan on 16/1/21.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Chatbubble:uiimageview
Text to display
@property (nonatomic, copy) NSString * text;
@end
/******************************************/
//
Chatbubble.m
Chatbubble
//
Created by Big Huan on 16/1/21.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//
#import "ChatBubble.h"
Text distance picture left margin
static const CGFloat Kleftmargin = 20;
Text distance picture, bottom, right margin
static const CGFloat Kothermargin = 10;
@interface chatbubble ()
@property (nonatomic, Strong) UILabel * Textlabel;
@end
@implementation chatbubble
-(UILabel *) Textlabel {
if (!_textlabel) {
_textlabel = [[UILabel alloc] init];
_textlabel.font = [Uifont systemfontofsize:17];
_textlabel.textcolor = [Uicolor blackcolor];
_textlabel.numberoflines = 0;
}
return _textlabel;
}
-(Instancetype) initWithFrame: (CGRect) Frame {
if (self = [Super Initwithframe:frame]) {
Stretch picture
Self.image = [UIImage imagenamed:@ "Qipao"];
Self.image = [Self.image resizableimagewithcapinsets:uiedgeinsetsmake (5, 5) Resizingmode: Uiimageresizingmodestretch];
Load label
[Self AddSubview:self.textLabel];
}
return self;
}
-(void) SetText: (NSString *) Text {
_text = text;
_textlabel.text = text;
}
-(Cgsize) Sizethatfits: (cgsize) Size {
Calculate the width of a label
CGFloat width = size.width-kleftmargin-kothermargin;
Calculates the height of a label based on the width of the label
Cgsize Sizelabel = [_textlabel sizethatfits:cgsizemake (width, maxfloat)];
Set the frame of the label
_textlabel.frame = CGRectMake (Kleftmargin, Kothermargin, Sizelabel.width, sizelabel.height);
CGFloat imagewidth = size.width;
Calculate the height of the ImageView
CGFloat imageheight = kothermargin * 2 + _textlabel.frame.size.height;
Return Cgsizemake (ImageWidth, imageheight);
}
@end
/************************************************************/
//
Viewcontroller.m
Chatbubble
//
Created by Big Huan on 16/1/21.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//
#import "ViewController.h"
#import "ChatBubble.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
CGFloat width = self.view.frame.size.width-60;
chatbubble * bubble = [[Chatbubble alloc] init];
Bubble.text = @ "Initial investment of $6.6 billion, the final length of 400 miles, a total investment of $33 billion, equivalent to 82.5 million dollars per mile." The cost of the super-high-iron project is about $20 million per mile,?? ";
Cgsize size = [Bubble sizethatfits:cgsizemake (width, maxfloat)];
Bubble.frame = CGRectMake (Size.width, size.height);
[Self.view addsubview:bubble];
}
@end
/******************************************************/
iOS common technology-bubble text adaptive