Objective-C - How to use UILabel on ios develop

tag: Objective-C    date: 2018-10-12

create a new uilabel for test

UILabel *label = [[UILabel alloc] initWithFrame:(CGRect){10,30,300,100}];
label.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu.";
label.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:label];
create uilabel

uilabel set text alignment

label.textAlignment = NSTextAlignmentCenter; // left or center or right
uilabel text alignment

uilabel set multiple lines

label.numberOfLines = 0; // 0 means multiline
uilabel  multiline

uilabel set font and font size

label.font = [UIFont fontWithName:@"AmericanTypewriter" size:21];
uilabel font

uilabel set bold font

label.font = [UIFont boldSystemFontOfSize:21];
uilabel bold

uilabel set underline

NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:@"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu."];
[attributedString addAttribute:NSUnderlineStyleAttributeName
                         value:@(NSUnderlineStyleSingle)
                         range:(NSRange){0,attributedString.length}];
[attributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedString.length)];
[label setAttributedText:attributedString];
uilabel underline

uilabel set text shadow

label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(1,1);
uilabel text shadow

uilabel set uppercase

label.text = [label.text uppercaseString];

uilabel set line spacing

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10.0f; // change this value
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
label.attributedText = attributedString;
uilabel line spacing

uilabel set character spacing - kerning

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
float spacing = 5.0f;
[attributedString addAttribute:NSKernAttributeName value:@(spacing) range:NSMakeRange(0, [label.text length])];
label.attributedText = attributedString;
[self.view addSubview:label];
uilabel character spacing

show emoji on uilabel

label.text = @"You can use ⌃ ⌘ Space shortcut to show the symbols panels and just insert the emoji you're looking for.😀";
uilabel emoji

uilabel set clickable link - hyperlink

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"https://www.google.com" attributes:nil];
[attributedString setAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}
                          range:NSMakeRange(0, attributedString.length)];
label.attributedText = attributedString;
uilabel hyperlink

uilabel handle click event

label.userInteractionEnabled = YES; // don't forget
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[label addGestureRecognizer:tap];
-(void) tap:(UITapGestureRecognizer*)gesture{
    [[[UIAlertView alloc] initWithTitle:@"demo"
                                message:@"UILabel Taped"
                               delegate:self
                      cancelButtonTitle:@"cancel"
                      otherButtonTitles:nil] show];
}
uilabel click event

uilabel set background image

label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.jpg"]];
uilabel background image

uilabel set border and border color

label.layer.borderWidth = 2.0f;
label.layer.borderColor = [UIColor blueColor].CGColor;
uilabel border

uilabel set rounded corners - border radius

label.layer.cornerRadius = 8.0f;
label.layer.masksToBounds = YES;
uilabel rounded corners

uilabel set gradient background

label.backgroundColor = [UIColor clearColor];
UIView *gradientView = [[UIView alloc] initWithFrame:label.frame];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = label.bounds;
gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor redColor].CGColor];
[gradientView.layer insertSublayer:gradient atIndex:0];
[self.view insertSubview:gradientView atIndex:0];
uilabel gradient background

rotate an uilabel

[label setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
uilabel rotate

uilabel calculate dynamic height - get height of text

label.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
NSLog(@"Label height:%f", label.bounds.size.height);
CGSize size = [label sizeThatFits:CGSizeMake(label.bounds.size.width, CGFLOAT_MAX)];
NSLog(@"Label height needed:%f", size.height);
Here is the output:
2018-10-12 14:09:05.333267+0800 Demo[4069:180367] Label height:100.000000
2018-10-12 14:09:05.337475+0800 Demo[4069:180367] Label height needed:304.500000

more articles
Copyright © 2018
This Site is Licensed under a Creative Commons License. Powerd by Java