"IOS" aligns text at the bottom of different font sizes

Source: Internet
Author: User

From WP to IOS, or put down ...

In the project, to achieve a number of different sizes of text bottom alignment effect as follows:

(Desired effect)

Think that with three uifont different uilabel after the bottom of their alignment can be, but the effect is as follows:

(unwanted effect)

The bottom is completely not aligned Ah, why "1314" is higher than the two sides of so many AH!!!! Compulsive disorder can not endure!!!

--------------------------------------------------------------------------------

Contrast:

1. Control is relative layout in Windows Phone

Windows phone inside TextBlock need to set a fixed width and height (at least I do)

TextBlock not only can you set text to center horizontally, to the left, to the right, but also through properties

Text. Verticalcontentalignment=top/center/bottom put text on top/center/bottom.

2. Controls in iOS are absolute layouts: the frame (including start position, height, and width) for which a control needs to be set

You can get the adaptive height and width of a string directly by using the following methods:(this feels better than WindowsPhone)

Cgsize labelsize = [@ "1314" Sizewithattributes:@{nsfontattributename:klabelfont}];

Native Uilabel can only set the horizontal offset property of the TextAlignment text, and cannot directly change the offset in the vertical direction.

WindowsPhone in order to achieve the above effect, just three TextBlock bottom, TextBlock content, can be aligned.

(This is suddenly I am not sure, it seems that many times I have directly set the margin to manually align, there is no time to WindowsPhone to see the changes. )

Detour:

So when iOS deals with this problem, I think that since the bottom of the three controls is aligned, it's just a different height, can it be modeled after WindowsPhone,

Offset the contents of the higher Uilabel ("1314") vertically to the bottom?

After consulting Uilabel found these:

cannot be called directly only through inheritance to override

Override points. Can adjust rect before calling super. (the position of the text content can be adjusted before calling the parent class Textrect)

Label has default content mode of Uiviewcontentmoderedraw

-(CGRect) Textrectforbounds: (cgrect) Bounds Limitedtonumberoflines: (Nsinteger) NumberOfLines; //rewrite to redraw text area

-(void) Drawtextinrect: (cgrect) rect; //override to redraw text overrides when calling super can be drawn by default drawing properties

So in order to change the position of text, we just need to change the Textrectforbounds's return value rect, and redraw the text based on this rect.

1, about Contentmode, this mode provides a variety of modes for the view to apply to the frame rectangle. You can set this value if you do not meet the requirements for a pattern other than uivewcontentmoderedraw, such as Uiviewcontentmodescaletofill, or if you have a specific need to customize the drawing view . The default is to automatically call Setneedsdispllay or Setneedsdisplayinrect when the view adapts to the frame rectangle: to redraw the view.
2, when sending a setneedsdisplay (or setneedsdisplayinrect:) message to the view, the DrawRect is forced to be invoked regardless of the mode contentmode at this point:

Main code: (This code on the Internet a lot, find their own)

-(CGRect) Textrectforbounds: (cgrect) Bounds Limitedtonumberoflines: (nsinteger) numberoflines{ //Parameters in the method bounds Refers to the Uilabel bounds cgrect RC=[Super Textrectforbounds:bounds Limitedtonumberoflines:numberoflines]; //text's bounds Switch(_verticalalignment) { CaseVERTICALALIGNMENTTOP:RC.ORIGIN.Y=BOUNDS.ORIGIN.Y;  Break;  CaseVERTICALALIGNMENTBOTTOM:RC.ORIGIN.Y= BOUNDS.ORIGIN.Y + Bounds.size.height-Rc.size.height;  Break;  CaseVerticalalignmentmiddle:default: Rc.origin.y= Bounds.origin.y + (bounds.size.height-rc.size.height)/2;  Break; }     returnRC;}- (void) Drawtextinrect: (cgrect) rect{CGRect RC=[self textrectforbounds:rect limitedToNumberOfLines:self.numberOfLines]; [Super DRAWTEXTINRECT:RC];}

However, it is found that the VerticalAlignment attribute is basically not very different for the adaptive wide-height (cgsize) label.

Origin.x origin.y are both zero and the size difference is almost not all and will not have much difference.

Here I made a comparison: for the string @ "1314", set different uifont to observe the difference in size between the Uilabel and the internal textrect

Uifont 1 50 100

RC (text) 1.000000,2.333333 105.666667,61.000000 208.666667,120.666667

Bounds (label) 1.000000,2.193359 105.541992,60.667969 208.349609,120.335938

Difference 0, 0.139974 0.124675,0.332031 0.317058, 0.330729

RC is still a little bit bigger than bounds, I think the text size is still smaller than the text border for the text not to be intercepted ,

And for different sizes of uifont or font types, this small degree is not the same.

So even setting the VerticalAlignment property doesn't make any difference.

If you only set the text size larger, resulting in RC larger than bounds, then the text portion may be intercepted;

If only increase the size of the Uilabel, resulting in RC smaller than bounds, then you will see in addition to the text outside the extra white space;

For the latter two, setting the VerticalAlignment property, you can obviously see the upper and lower offsets.

So this scheme is not valid for achieving the desired effect.

End:

Find it and find it. Another way to make Uilabel display Rich text through the Attributedtext property

Implements a string that contains different colors, font sizes, and so on.

I encapsulated a method:

//get text content with different styles//Stringarray Array of strings//Attributeattay Style Array-(Nsattributedstring *) Attributedtext: (nsarray*) Stringarray Attributeattay: (Nsarray *) attributeattay{//define the text content to displayNSString *string= [Stringarray componentsjoinedbystring:@""]; //Splice An array of incoming strings //Create a String object with an attribute style by the text content you want to displaynsmutableattributedstring * result = [[Nsmutableattributedstring alloc] initwithstring:string];  for(Nsinteger i =0; i < Stringarray.count; i++){
     //Set a range of strings in a style [Result Setattributes:attributeattay[i] range:[
stringRangeofstring:stringarray[i]]; } //returns the styled text that has been set return[[Nsattributedstring alloc] initwithattributedstring:result];}

and the use

Nsdictionary *attributesextra =@{nsfontattributename:klabelfont,//font size Nsforegroundcolorattrib  Utename: [Uicolor Orangecolor]}; Nsdictionary *attributesprice =@{nsfontattributename:kpricefont,//font size Nsforegroundcolorattri  Butename: [Uicolor Orangecolor]}; Nsattributedstring*attributedstring = [Self attributedtext:@[@"¥", @ "1314",@"from"] [Attributeattay:@[attributesextra,attributesprice,attributesextra]]; UILabel *price = [[UILabel alloc]init];
Price.attributedtext
=attributedstring; CGRect rect= [Attributedstring boundingrectwithsize:cgsizemake (Self.width/2, -) Options:nsstringdrawinguseslinefragme Ntorigin Context:nil]; //This method gets to the adaptive rect instead of the cgsize maximum size value of Cgsizemake (SELF.WIDTH/2, 100)
Price.frame= CGRectMake (0,0,rect.size.width, rect.size.height);

"IOS" aligns text at the bottom of different font sizes

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.