How to correctly implement line spacing and row heights in iOS

Source: Internet
Author: User

Https://juejin.im/post/5abc54edf265da23826e0dc9

Recently, a new lineheight attribute is added to the text element of Virtualview-ios to ensure a more accurate two-platform consistency in conjunction with Virtualview-android. After programming for Google and Stack Overflow, it was discovered that most of the data you can find is about how to implement linespacing properties, not lineheight. But I just want to implement a lineheight because IOS and Android default linespacing are inconsistent. Still need to self-clothed, incidentally organized into articles for the benefit of posterity.

About Line Spacing linespacing

First put out a default layout style for UILabel in IOS:

As you can see, in the default layout style, the line spacing of the text is very small and the text is very crowded.

At a time like this, the designer will put forward the need for line spacing, hoping to make the text appear more beautiful. A similar callout would look like this:

Generally speaking, since the designer is asking for the line spacing, then we can set the linespacing directly. But UILabel is not such a direct exposed attribute, want to modify linespacing, we need to use nsattributedstring to implement, schematic code:

setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

Run a look at the results:

Although using our eyes seems to be no problem, but the designer's eyes can be seen, and the design of the requirements of the gap:

How could this be so!? It's not the same as the deal, right? Don't panic, I'll explain it carefully.

Correct implementation of line spacing

Look first:

The red area is the area where the default drawing of a single line of text will occupy, and you can see that there are some white (blue and red overlapping parts) up and down the text. The designer wants the blue area height to be 10pt, and we directly set the linespacing to set the Green Zone height in the middle of the two rows of red areas to 10pt, which is the root of the problem.

So what is the height of this red area? The answer is label.font.lineHeight that it is the original line height that draws a single line of text using the specified font.

The problem is solved after knowing the reason, we need to subtract the system's own margin when setting LineSpacing:

setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

Take a look at the effect, perfect fit:

About Row Height Lineheight

If you only care about the text on your IOS device, it's enough to see it. But what I need is that IOS and Android show exactly the same effect, so the line spacing is not enough. The main reason is also mentioned in the preface, the text on the Android device is left white (the blue and red overlapping parts of the previous section) and the IOS device is inconsistent:

On the left is the IOS device, the right Android device, you can see the same 20th number of fonts, Android will be a bit higher line height. The fonts used on different Android devices are not the same, and there may be more differences. If you do not find a way to smooth the difference, you can not realize the true meaning of the double-ended consistent.

At this time we can set lineheight to make each line of the height of the same, Lineheight set to 30pt, a line of text height must be 30pt, two lines of text height must be 60pt. Although there will be subtle differences in the rendering of text, the differences in layout will be completely erased. Lineheight can also be implemented with the help of nsattributedstring, the schematic code:

setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

Run a look at the results:

In debug mode, it is confirmed that the height of the following is true, but why is the text displayed at the bottom of the line?

Fixed the position of text when line height increased

Fixed the position of text displayed in line, we can use the BaselineOffset property to fix. This property is useful and is often used when you want to implement a requirement such as superscript. After debugging, find the most appropriate value is (lineHeight - label.font.lineHeight) / 4 (not yet figure out why divided by 4 instead of divided by 2, want to know the old driver pointing twos). The final code example is as follows:

setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];CGFloat baselineOffset = (lineHeight - label.font.lineHeight) / 4;[attributes setObject:@(baselineOffset) forKey:NSBaselineOffsetAttributeName];label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:attributes];

Paste in different font size and line to show the effect:

An issue when row height and line spacing are used at the same time

I have to say line height and line spacing we have all been able to achieve the perfect, but when I try to use them at the same time, I found a bug in IOS (of course, it may be a feature, after all, not crash is not necessarily a bug):

The shaded area is the drawing area of the text, where the area that appears to be Orange is linespacing, and the green area is lineheight. But why is a single-line text system also to show a linespacing Ah!? Where's The pit daddy?

Fortunately we are usually row height and line spacing are used independently for different requirements, and they do not trigger this problem when used separately. So in the Virtualview-ios library, I'm going to keep the logic of high computational consistency in line with the system.

Summarize

At this point, the success of the Virtualview-ios added to the Lineheight attribute support, more implementation details you can go to the open Source library directly to see the source code. Hope that our Tangram program can be more perfect, to help more people at a time to develop both ends of the use, with a piece of tangram to spell out the universe.

How to correctly implement line spacing and row heights in iOS

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.