Hello friends,
This is below my code.
@interface DynamicLabel : UILabel
{
}
- (id) getSize:(NSString *)text FontName:(NSString *)f_name FontSize:(float)f_size label: (UILabel *)templbl;
@end
#import "DynamicLabel.h"
#define DEFAULT_COLOR [UIColor blackColor]
@implementation DynamicLabel
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
}
return self;
}
//function for dynamically get size of label
- (id) getSize:(NSString *)text FontName:(NSString *)f_name FontSize:(float)f_size label:(UILabel *)templbl
{
templbl.numberOfLines = 0;
[templbl setFont:[UIFont fontWithName:f_name size:f_size]];
[templbl setLineBreakMode:UILineBreakModeWordWrap];
CGSize maximumLabelSize = CGSizeMake(310,9999);
CGSize expectedLabelSize = [text sizeWithFont:templbl.font
constrainedToSize:maximumLabelSize
lineBreakMode:templbl.lineBreakMode];
CGRect newFrame = templbl.frame;
NSLog(@"New Frame:--->%@",NSStringFromCGRect(newFrame));
newFrame.size.height = expectedLabelSize.height;
NSLog(@"New Frame Height:--->%f",newFrame.size.height);
templbl.frame = newFrame;
NSLog(@"Temp Label Frame:--->%@",NSStringFromCGRect(templbl.frame));
[templbl setText:text];
NSLog(@"Temp Label Text:---%@",templbl.text);
[templbl sizeToFit];
return templbl;
}
I used above code in RootViewController and success dynamically get height of label
#import "DynamicLabel.h"
@interface RootViewController : UIViewController
{
DynamicLabel *dlabel;
}
@property (nonatomic,retain) DynamicLabel *dlabel;
-(void)dynamiclabel;
@end
#import "RootViewController.h"
#define LABELS_FONT_NAME_BOLD @"Helvetica-Bold"
#define FONT_GREEN_COLOR [UIColor colorWithRed:0.122f green:0.416f blue:0.20f alpha:1.0f]
@implementation RootViewController
@synthesize dlabel;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Label Navigation Demo";
}
-(void)dynamiclabel
{
dlabel = [[DynamicLabel alloc] initWithFrame:CGRectMake(30, 130, 300, 20)];
[dlabel getSize:@"Choose a topic from the left to find answers for your questions about iPhone. For the latest downloads, manuals, and other resources for iPhone, choose an option below."
FontName:LABELS_FONT_NAME_BOLD FontSize:12.5f label:dlabel];
dlabel.textColor = FONT_GREEN_COLOR;
dlabel.backgroundColor = [UIColor clearColor];
NSLog(@"Dynamic Label Height:--->%f",dlabel.frame.size.height);
[self.view addSubview:dlabel];
}
My issue is that I want to add custom button dynamically wherever iPhone text is displayed in label text. Is there any way to develop this functionality?
Thanks in advance.
You can create custom sized buttons for an arbitrary paragraph of text using stretchable images and
sizeWithFont:constrainedToSize:, but a design that needs explanation is not a good design, and a paragraph inside a button is a good example of that.