简体   繁体   中英

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.
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 *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. Do same for others. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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