简体   繁体   English

如何仅为 UITextView 的第一行设置不同的字体大小?

[英]How to set a different font size just for UITextView's first line of?

I got a UItextView , I want the first line of its content to have larger size than the rest.我有一个UItextView ,我希望其内容的第一行比其余内容的尺寸更大。
Like the following image.像下图。
Example例子
But I don't see any font size related attribute in paragraph attributes.但是我在段落属性中没有看到任何与字体大小相关的属性。
So, how to do that?那么,该怎么做呢?
Thanks for any advices.感谢您的任何建议。

Use NSMutableAttributedString..使用 NSMutableAttributedString..

            NSMutableAttributedString *firstLine = [[NSMutableAttributedString alloc]initWithString:@"First Line\n" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20]}];

            NSAttributedString *secondLine = [[NSAttributedString alloc]initWithString:@"Second Line" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]}];

            [firstLine appendAttributedString:secondLine];
            yourTextView.attributedText = firstLine;

Have you tried attributed String?您是否尝试过归因字符串? You just need to know how long your first line will be.你只需要知道你的第一行有多长。

// 16.0 is your standard font size for the textView
let text = NSMutableAttributedString(string: "your whole text for the textview", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16.0)])
// 23.0 is the size for your first line
text.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 23.0), range: NSRange(location: 0, length: firstLineLength))

textView.attributedText = text

Select your label then tap on the attributes inspector .选择您的标签,然后点击属性检查器

Select the Attributed选择属性

在此处输入图片说明

You will see the changes just like this -你会看到这样的变化——

在此处输入图片说明

Select the first line just like -选择第一行就像 -

在此处输入图片说明

Then tap on the text icon然后点击文字图标

在此处输入图片说明 - ——

Now change the font and press enter after that, changes will trigger in your stroyboard label.现在更改字体并在此之后按 Enter,更改将在您的 stroyboard 标签中触发。 Do same for others.为他人做同样的事情。 在此处输入图片说明

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

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