Apple iOS Uilabel text Marquee effect

Source: Internet
Author: User

In the actual development process, we will encounter such a situation, a sentence is too long, the display is not complete, the most typical is the navigation bar to display the title text, if too long text will appear incomplete situation, with Uilabel can achieve the effect of the horse, the text display integrity. The specific code is as follows:

The code is as follows Copy Code

#pragma mark-Animation
-(void) startanimationifneeded{
Cancel, stop all animations
[Titlelabel.layer removeallanimations]; The Titlelabel here is to implement the label of the marquee text

Cgsize size = Cgsizemake (320,2000); Set a row height limit

Cgsize textsize = [Titlelabel.text SizeWithFont:titleLabel.font];
Nsdictionary *attribute = @{nsfontattributename:titlelabel.font};
Cgsize textsize = [Titlelabel.text boundingrectwithsize:size options:nsstringdrawingtruncateslastvisibleline Attributes:attribute context:nil].size;

CGRect lframe = titlelabel.frame;
Lframe.size.width = Textsize.width;
Titlelabel.frame = Lframe;
Const float Oriwidth = 10;
if (Textsize.width > Oriwidth) {
float offset = textsize.width-oriwidth;
[UIView animatewithduration:18.0
delay:0
Options
Uiviewanimationoptionrepeat//Animation duplicate main switch
//         | Uiviewanimationoptionautoreverse//Animation repeat automatically reverse, need to use with the above this
| Uiviewanimationoptioncurvelinear//Animation time curve, scrolling subtitles linear more reasonable
animations:^{
Titlelabel.transform = cgaffinetransformmaketranslation (-offset-400, 0);
}
completion:^ (BOOL finished) {

}
];
}
}

Note: Running distance can be based on their own needs of the method to modify the speed of the animation can also be modified, the effect looks good.

Add another article

The code is as follows Copy Code

-(void) Viewdidload {
Uilabel *label1 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 20.0, 200.0, 50.0)];
Uilabel *label2 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 80.0, 200.0, 50.0)];
Uilabel *label3 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 140.0, 200.0, 50.0)];
Uilabel *label4 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 200.0, 200.0, 50.0)];
Uilabel *label5 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 260.0, 200.0, 50.0)];
Uilabel *label6 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 320.0, 200.0, 50.0)];
Uilabel *LABEL7 = [[Uilabel alloc]initwithframe:cgrectmake (50.0, 380.0, 200.0, 50.0)];
Set display text
Label1.Text = @ "Label1";
Label2.Text = @ "Label2";
Label3.text = @ "label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--";
Label4.text = @ "label4--label4--label4--label4--";
Label5.text = @ "label5--label5--label5--label5--label5--label5--";
Label6.text = @ "Label6";
Label7.text = @ "Label7";
Set font: Bold, Normal is systemfontofsize
Label1.font = [Uifont boldsystemfontofsize:20];
Set Text color
Label1.textcolor = [Uicolor Orangecolor];
Label2.textcolor = [Uicolor Purplecolor];
Set Text position
Label1.textalignment = Uitextalignmentright;
Label2.textalignment = Uitextalignmentcenter;
Set font size to fit label width
Label4.adjustsfontsizetofitwidth = YES;

Set the number of lines for a label
Label5.numberoflines = 2;
Uilabel.backgroudcolor=[uicolor Clearcolor]; You can remove the background color

Setting highlighting
label6.highlighted = YES;
Label6.highlightedtextcolor = [Uicolor Orangecolor];
Set Shadow
Label7.shadowcolor = [Uicolor Redcolor];
Label7.shadowoffset = Cgsizemake (1.0,1.0);
Setting whether to interact with the user

label7.userinteractionenabled = YES;
Sets whether the text in the label is variable and the default is Yes
label3.enabled = NO;
Set the display format for text that is too long

Label3.linebreakmode = uilinebreakmodemiddletruncation;//to intercept the middle
typedef enum {
Uilinebreakmodewordwrap = 0,
Uilinebreakmodecharacterwrap,
uilinebreakmodeclip,//, cut off the extra parts.
uilinebreakmodeheadtruncation,//, cut off the head.
uilinebreakmodetailtruncation,//, cut the tail.
uilinebreakmodemiddletruncation,//, intercept the middle.
} Uilinebreakmode;
If the Adjustsfontsizetofitwidth property is set to Yes, this property controls the behavior of the text baseline
Label4.baselineadjustment = Uibaselineadjustmentnone;
typedef enum {
Uibaselineadjustmentalignbaselines,
Uibaselineadjustmentaligncenters,
Uibaselineadjustmentnone,
} uibaselineadjustment;
[Self.view Addsubview:label1];
[Self.view Addsubview:label2];
[Self.view Addsubview:label3];
[Self.view Addsubview:label4];
[Self.view Addsubview:label5];
[Self.view Addsubview:label6];
[Self.view ADDSUBVIEW:LABEL7];
[Label1 release];
[Label2 release];
[Label3 release];
[Label4 release];
[Label5 release];
[Label6 release];
[Label7 release];
[Super Viewdidload];
}
/*
Override to allow orientations than the default portrait orientation.
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation {
Return YES to supported orientations
return (interfaceorientation = = uiinterfaceorientationportrait);
}
*/
-(void) didreceivememorywarning {
Releases the view if it doesn ' t have a superview.
[Super didreceivememorywarning];
Release any cached of data, images, etc that aren ' t in use.
}
-(void) Viewdidunload {
Release any retained subviews of the main view.
e.g. Self.myoutlet = nil;
}
-(void) Dealloc {
[Super Dealloc];
}
@end

Related Article

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.