【问题标题】:How do you bold the first line in an NSMutableParagraphStyle?你如何加粗 NSMutableParagraphStyle 中的第一行?
【发布时间】:2019-08-07 19:30:16
【问题描述】:

我有一个名为“矩形”的类来制作自定义 UILabel。我覆盖了矩形类中的“绘制”。当我实例化标签时,我希望第一行文本以粗体显示。我知道如何通过手动获取每个字符串的范围来解决这个问题……但是,我有 300 多个字符串要做。字符串当前位于一个数组中,格式如下:“Happy \n Birthday”。我怎样才能使“快乐”这个词变得粗体?

var messageText = "Happy \n Birthday"
let rectanglePath = UIBezierPath(rect: rectangleRect)
context.saveGState()
UIColor.white.setFill()
rectanglePath.fill()
context.restoreGState()

darkPurple.setStroke()
rectanglePath.lineWidth = 0.5
rectanglePath.lineCapStyle = .square
rectanglePath.lineJoinStyle = .round
rectanglePath.stroke()

let rectangleStyle = NSMutableParagraphStyle()
rectangleStyle.alignment = .center
let rectangleFontAttributes = [
  .font: UIFont.myCustomFont(true),
  .foregroundColor: UIColor.black,
  .paragraphStyle: rectangleStyle,
  ] as [NSAttributedString.Key: Any]

let rectangleTextHeight: CGFloat = messageText.boundingRect(with: CGSize(width: rectangleRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: rectangleFontAttributes, context: nil).height
context.saveGState()
context.clip(to: rectangleRect)
messageText.draw(in: CGRect(x: rectangleRect.minX, y: rectangleRect.minY + (rectangleRect.height - rectangleTextHeight) / 2, width: rectangleRect.width, height: rectangleTextHeight), withAttributes: rectangleFontAttributes)
context.restoreGState()

【问题讨论】:

  • 你为什么不找\n
  • 您能详细说明一下吗?我尝试迭代直到 \n 但它没有编译

标签: ios swift uilabel nsattributedstring nsrange


【解决方案1】:

您可以通过 newline 分隔字符串来找到第一个:

let firstLine = "Happy \n Birthday".split(separator: "\n").first

这将为您提供字符串的第一行。 (长文本多行不计算在内)然后您可以使用它找到范围并应用粗体效果。

这是如何工作的:

  • 您需要将标签设置为接受多行的方式:
  • 查找第一行的范围
  • 将其转换为nsRange
  • 将属性应用于范围

这是一个完整的示例:

import UIKit
import PlaygroundSupport

extension StringProtocol where Index == String.Index {

    func nsRange(from range: Range<Index>) -> NSRange {
        return NSRange(range, in: self)
    }
}

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.numberOfLines = 0
        label.text = "Happy \n Birthday"
        label.textColor = .black

        let text = "Happy \n Birthday"
        let attributedString = NSMutableAttributedString(string: text)
        let firstLine = text.split(separator: "\n").first!
        let range = text.range(of: firstLine)!
        attributedString.addAttributes([.font : UIFont.boldSystemFont(ofSize: 14)], range: text.nsRange(from: range))
        label.attributedText = attributedString
        label.sizeToFit()

        view.addSubview(label)
        self.view = view
    }
}

PlaygroundPage.current.liveView = MyViewController()

【讨论】:

  • 感谢您的回复。我试过了,还是不行……不知道怎么在一个矩形中显示两条短信?
  • 请问如何使用这个找到范围?
  • let firstLine = messageText.split(separator: "\n").first(String) let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)] let attributesString = NSMutableAttributedString (字符串:firstLine,属性:attrs)
  • 我给你加一个例子。
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 2013-01-19
  • 2016-11-04
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多