iOS common technology-bubble text adaptive

Source: Internet
Author: User

//
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.