简体   繁体   English

动态标签文字,行间有空格+自定义线宽

[英]Dynamic label text with space between lines + custom width of line

I am trying to achieve something similar to 我正在尝试实现类似于 理想的结果

currently, my posts are collected from a database and are put into a label, as such. 目前,我的帖子是从数据库中收集的,并放置在标签中。

当前标签

I have tried using attributed strings and changing the background colors of the text - however, I would like to achieve what is in the picture with spaces between the lines and custom width for each line of text. 我尝试使用属性字符串并更改文本的背景颜色-但是,我想实现图片中的内容,其中行之间有空格,并且每行文本都具有自定义宽度。 How would I achieve this effect with dynamic text? 如何使用动态文本实现此效果?

Step 1) Break a string up into lines. 步骤1)将字符串分成几行。 You have to know the width of your target view. 您必须知道目标视图的宽度。 view.bounds.width will do. view.bounds.width可以。

See link Question to show you how to break up the text into lines. 请参阅链接问题 ,向您展示如何将文本分成几行。 Pay attention to the question not the answer although that will be part of the answer on the next step. 注意问题而不是答案,尽管这将是下一步答案的一部分。

Step 2) Ditch UILabel. 步骤2)放下UILabel。 Use UITextView at the very least. 至少使用UITextView。 Apple changed UILabel to pull down orphan words so even if you get the lines right, a UILabel may not give you the look of what you are doing since it could pull a word down. Apple更改了UILabel来拉下孤立的单词,因此即使您正确理解了行,UILabel也不会让您看起来在做什么,因为它可能会拉下一个单词。 Use a UITextView and disable scroll, editing disabled, and possibly selection disabled. 使用UITextView并禁用滚动,禁用编辑以及可能禁用的选择。 That will give you intrinsic size to lay it out like a UILabel. 这将给您固有的大小,使其像UILabel一样进行布局。

Step 3) Use attributed String to add the backgroundColor/highlighting you seek or use multiple UITextViews(possibly with a UIStackView) or CATextLayers with manual layout to divide your lines up in their own view or layer with the spacing you desire and just use the backgroundColor property. 步骤3)使用属性字符串添加要查找的backgroundColor /突出显示,或使用具有手动布局的多个UITextViews(可能与UIStackView一起使用)或CATextLayers,以所需的间距在自己的视图或图层中划分行,仅使用backgroundColor属性。 That's up to you. 随你(由你决定。 I don't know what you are after. 我不知道你在追求什么

Sorry I did not show you exactly and only pieced together snippets but this is how you do it. 抱歉,我没有完全向您展示,只是拼凑了片段,但这是您的操作方式。 Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM