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

问题描述 投票:-1回答:3

我有一个UItextView,我希望其内容的第一行比其他内容更大。 如下图所示。 Example 但我没有在段落属性中看到任何与字体大小相关的属性。 那么,该怎么做? 感谢您的任何建议。

ios objective-c swift uitextview nsattributedstring
3个回答
0
投票

使用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;

0
投票

你尝试过归因于String吗?你只需要知道你的第一行将会有多长。

// 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

0
投票

选择标签,然后点击属性检查器。

选择归因

enter image description here

你会看到这样的变化 -

enter image description here

选择第一行就像 -

enter image description here

然后点击文本图标

enter image description here-

现在更改字体并按Enter键,更改将在故事板标签中触发。为别人做同样的事。 qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.