【问题标题】:How to set a space between characters and lines in UILabel如何在 UILabel 中的字符和行之间设置空格
【发布时间】:2013-07-13 16:40:06
【问题描述】:

我想在第一行和第二行之间设置一个间距,其他行之间需要另一个间距。这样,第二行和下一行必须有特定的字符间距。

这一切都需要在一个控件中完成。我怎么能这样做?我决定为每一行创建一个单独的UILabel,但我认为这是错误的方式。

【问题讨论】:

    标签: ios cocoa-touch uilabel


    【解决方案1】:

    您无法更改文本行之间的间距,您必须继承 UILabel 并滚动您自己的 drawTextInRect、创建多个标签或使用不同的字体。

    但有两个自定义标签,可让您控制线高。

    1) https://github.com/LemonCake/MSLabel

    2) https://github.com/Tuszy/MTLabel

    希望这会有所帮助...

    在 iOS6 中,您可以这样做:

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:40];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    cell.label.attributedText = attributedString ;
    

    【讨论】:

    • 感谢您的快速回答。可能必须将每一行绘制为单独的控件
    • 用 \n 分割文本中的每一行,然后实现这个单独的控件。
    • 除CGContextSetCharacterSpacing外,如何改变字母间距?
    • 我只知道CGContextSetCharacterSpacing: 改变字符间距,有没有别的办法?
    【解决方案2】:

    试试这个,它应该适合你(使用 Swift 4)

    let label = UILabel()
    let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"
    let attrString = NSMutableAttributedString(string: stringValue)
    var style = NSMutableParagraphStyle()
    style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
    style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
    attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
    // add strike
    attrString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attrString.length))
    // add space between characters
    attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))
    label.attributedText = attrString
    


    结果:

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多