【问题标题】:How to set a different font size just for UITextView's first line of?如何为 UITextView 的第一行设置不同的字体大小?
【发布时间】:2019-02-13 11:30:23
【问题描述】:

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

【问题讨论】:

标签: ios objective-c swift uitextview nsattributedstring


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你试过属性化字符串吗?你只需要知道你的第一行会有多长。

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

    【讨论】:

      【解决方案3】:

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

      选择属性

      你会看到这样的变化——

      选择第一行就像-

      然后点击文本图标

      -

      现在更改字体,然后按 Enter,更改将在您的 stroyboard 标签中触发。为他人做同样的事情。

      【讨论】:

        猜你喜欢
        • 2011-03-04
        • 2017-08-24
        • 2012-07-20
        • 2017-09-15
        • 2011-03-05
        • 1970-01-01
        • 2018-08-24
        • 2021-03-11
        • 1970-01-01
        相关资源
        最近更新 更多